Info

This question is closed. Reopen it to edit or answer.

How to use IF equation in Matlab instead of Excel?

1 view (last 30 days)
Erik Hausen
Erik Hausen on 21 May 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a 8760x5 matrix (Production) and a 8760x1 column vector (Demand). The first row needs to be unchanged, but every row after that I need to follow an IF equation. For example row 2 of that equation needs to be if the value on row 2 of the production matrix equals the value of row 2 of the demand matrix, then the new value of that point on the matrix needs to be 'Demand row 2' - 'Demand row 1' + 'the value on the production matrix above it'.
If the value on that row of the production matrix doesn't equal the value in that row of the Demand matrix then the new value in the matrix should be 'the value on the production matrix above it.
In Excel instead of having a matrix I would just have 5 different production columns (A-E) and 1 Demand column (F) The equation for row 2 column A would be... IF(A2=F2, F2-F1+A1, A1)... and then I would just copy this formula from all cells from A2 to D8760. I am sorry this came out more confusing then I had hoped. Thanks for your help!

Answers (1)

dpb
dpb on 21 May 2015
Edited: dpb on 21 May 2015
Caution: "Air code", untested...
d=diff(D); % the demand differences to use first...
ix=bsxfun(@eq,P(2:end),D(2:end)); % Logical array of matches
P(ix)=d(ix)+P([ix(2:end;false(1,size(P,2)]);
NB: The expression [ix(2:end;false] for the row address serves to move the logical vector one row up to address the prior row from the target assigned row. Must augment by the false value to maintain the total length consistent.

Community Treasure Hunt

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

Start Hunting!