How can I perform simple addition/subtraction operations on only certain elements of a row/column based on the first row/column values?
Afficher commentaires plus anciens
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.
Réponses (1)
Image Analyst
le 21 Sep 2018
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 commentaires
Chris Butler
le 21 Sep 2018
Steven Lord
le 21 Sep 2018
Nowhere in your code did you change ValueTable. I suspect you expected that ValueTable had some sort of "reference", "pointer", or "handle" to the FirstColVolt and TopRowVolt arrays and that changing those arrays would also change ValueTable. That's not the case.
When you make a copy of those arrays (as you did when you created ValueTable) the copies are independent of the original FirstColVolt and TopRowVolt arrays. See the "Behavior of MATLAB Built-In Classes" section on this documentation page for a simpler example with an int32 scalar. Just as changing a in that example didn't change b, so changing FirstColVolt will not change ValueTable.
Image Analyst
le 21 Sep 2018
You don't need to assign ValueTable in advance if you don't want to. I don't see any point in it. As long as TopRowVolt and FirstColVolt still have the same number of elements, you create ValueTable AFTER you execute the code I gave. Like Steve said, if you make it up before, it won't automatically update when you change variables that initially went into creating it.
As an analogy, if I took a photo of you and a photo of your wife and put them into a photo album, and then I took another photo of you and another photo of your wife, would the album automatically change? No, of course not. I'd have to put the new photos in there if I wanted the album to change.
Chris Butler
le 22 Sep 2018
Chris Butler
le 22 Sep 2018
Image Analyst
le 22 Sep 2018
mask creates a logical vector (1-D array) where the value is "true" where the criteria is met and "false" where the criteria is not met. If you pass mask into an array as a logical index, then the operations you do apply ONLY to the elements where the mask is "true" and NOT to the elements where mask is false.
I'm not really sure what you want to do with those matrices. TopRowVolt is a 10-by-9 matrix, and FirstColVolt is a 10-by-1 vector.
Anyway, I discovered a problem with the code. Some of the earlier operations pushed the values into ranges where they were included in the mask in later operations. So I changed the code to check against only the original array values, not the "current" values which might have been changed.
FirstColVoltOriginal = FirstColVolt; % Make a copy of the original.
mask = FirstColVoltOriginal < 1;
FirstColVolt(mask) = FirstColVoltOriginal(mask) - 0.5;
mask = FirstColVoltOriginal >= 1 & FirstColVoltOriginal < 2;
FirstColVolt(mask) = FirstColVoltOriginal(mask) + 0.2;
mask = FirstColVoltOriginal >= 2;
FirstColVolt(mask) = FirstColVoltOriginal(mask) + 0.5;
TopRowVoltCopy = TopRowVolt; % Make a copy of the original.
mask2 = TopRowVoltCopy < 2;
TopRowVolt(mask2) = TopRowVoltCopy(mask2) - 0.5;
mask2 = TopRowVolt > 3;
TopRowVolt(mask2) = TopRowVoltCopy(mask2) + 0.5;
Chris Butler
le 22 Sep 2018
Chris Butler
le 22 Sep 2018
Chris Butler
le 22 Sep 2018
Modifié(e) : Chris Butler
le 22 Sep 2018
Chris Butler
le 22 Sep 2018
Catégories
En savoir plus sur Structures dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

