How can I perform simple addition/subtraction operations on only certain elements of a row/column based on the first row/column values?
1 次查看(过去 30 天)
显示 更早的评论
Basically, I have created a table with a range of voltage values across the top row and first column and a fixed 12 volts in every other element. Based on those varying values, I would like to modify the fixed elements to create the proper voltage in each element. All while ignoring the 0 value in (1,1).
I have this code so far, basically creating the table:
TopRowVolt = [linspace(0,5,9); linspace(12,12,9);linspace(12,12,9); ...
linspace(12,12,9);linspace(12,12,9);linspace(12,12,9);...
linspace(12,12,9);linspace(12,12,9);linspace(12,12,9);...
linspace(12,12,9)];
FirstColVolt = [0,linspace(0,3,9)]';
ValueTable = [FirstColVolt , TopRowVolt]
For instance, I want to subtract 0.5V across the table for any value in the first column less than 1V, add 0.2V for values in the first column that fall in the range 1V to 2V, and add 0.5V for values above 2V.
Likewise, based on the top row, I'd like to subtract 0.5V based on values less than 2V and add 0.5V on values greater than 3V. I don't understand how to get the code to determine which values require which action and then perform the specific action only on the proper rows/columns, as the first column and row need to be left alone.
0 个评论
回答(1 个)
Image Analyst
2018-9-21
Just use create a mask and use it as an index:
mask = FirstColVolt < 1;
FirstColVolt(mask) = FirstColVolt(mask) - 0.5;
mask = FirstColVolt >= 1 & FirstColVolt < 2;
FirstColVolt(mask) = FirstColVolt(mask) + 0.2;
mask = FirstColVolt >= 2;
FirstColVolt(mask) = FirstColVolt(mask) + 0.5;
mask2 = TopRowVolt < 2;
TopRowVolt(mask2) = TopRowVolt(mask2) - 0.5;
mask2 = TopRowVolt > 3;
TopRowVolt(mask2) = TopRowVolt(mask2) + 0.5;
10 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call MATLAB from C 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!