Modifying matrix with if statement

Hello, Im trying to modify a matrix with if statements, not sure if Im approaching it the correct way. Here's the problem, I have a variable nn=6 in this case, and two matrices of size (nn,2).
first matrix is called indi:
0 0
1 2
3 4
5 0
6 7
8 9
second matrix is made in the first for loop shown and gives the following result:
-5 10.416
0 0
0 0
0 14.583
0 0
0 0
What Im trying to achieve is to get the second for loop to remove the rows of the second matrix in which both values are zeros. So what i need is just basically:
-5 10.416
0 14.583
The error I get is "index exceeds matrix dimensions" which is probably happening since the second for loop goes from 1 to nn, and as it cycles the matrix stops being of size (nn,2). Anyone have any ideas on how to get around this??
here's the code:
for ii=1:nn
if indi(ii,1)==0
reacc(ii,1)=nodos(ii,1);
else
reacc(ii,1)=0;
end
if indi(ii,2)==0
reacc(ii,2)=nodos(ii,2);
else
reacc(ii,2)=0;
end
end
for jj=1:nn
if (reacciones(jj,1)==0) & (reacciones(jj,2)==0)
reacciones(jj, :) = []
end
end

 採用された回答

Roger Stafford
Roger Stafford 2014 年 11 月 12 日

0 投票

To correct the problem you describe, do that last for-loop backwards:
for jj=nn:-1:1
if ....

1 件のコメント

Alejandro
Alejandro 2014 年 11 月 12 日
I appreciate the help!! Thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 11 月 12 日

コメント済み:

2014 年 11 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by