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.

답변 (1개)

Image Analyst
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
Chris Butler
Chris Butler 2018년 9월 22일
편집: Chris Butler 2018년 9월 22일
This is what I'm actually working on here. And I've done it already with if/else statements; which was brutal. I gave an example problem that was similar to what I'm doing but different in size, values, variation, etc. I'm trying to learn the code, as opposed to have someone else write it for me, if that makes sense. It's an air/fuel ratio table based on engine temperature and throttle position. As you can see, my variable voltages for throttle position stay their original values across the top row, as do the variable voltages for engine temperature down the first column. Also, the original value for every engine temp./throttle pos. combination was a stoichiometric 14.7 and each value has been varied by both the engine temp. voltage and throttle position voltage so they have a gradual variation from one column/row to the next. (The "0" in (1,1) has been ignored bt the rest of the table.)
Chris Butler
Chris Butler 2018년 9월 22일
Like I previously mentioned, and displayed I guess, I've already figured out a way to accomplish this particular task (with a brutal 142 lines of code for such a task). But I would like to understand how to manipulate arrays more efficiently going forward. The screenshot I've shared here was a school project and that's why I didn't want to give my original problem, because as I stated earlier, I wanted to learn it instead of having someone else write it for me. (My course hasn't even reached masks yet to be honest. That's why I'm clueless as to how to use them.) Thanks so much, by the way for taking the time to try and help me understand this. And sorry if I don't soak this material up very well. I'm a hardcore novice at this.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call MATLAB from C에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by