フィルターのクリア

Is it possible to combine "for" and "if" like this?

1 回表示 (過去 30 日間)
Rachuan
Rachuan 2014 年 9 月 4 日
コメント済み: Rachuan 2014 年 9 月 4 日
Hello, Iam new at computing and matlab, I have a problem with the next part of my program.
nodos = [0 0 0 0 1 1; 48 0 0 0 1 1;84 0 0 0 1 1;48 36 -500 0 0 0]
Nnodos=4
for i=1:Nnodos
w=0
if nodos(i,5)==0
nodos(i,7)=w+1
w=w+1
end
end
When i run the program the matrix "nodos" is not modified. Is the i going from 1 to Nnodos?
thanks for your time.
  1 件のコメント
Matt J
Matt J 2014 年 9 月 4 日
When I run your code, nodos does get modified. A 7th column now appears, and it looks like the following
nodos =
0 0 0 0 1 1 0
48 0 0 0 1 1 0
84 0 0 0 1 1 0
48 36 -500 0 0 0 1

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

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 9 月 4 日
編集済み: Geoff Hayes 2014 年 9 月 4 日
Rachuan - when I run the code, the matrix nodos is modified. In fact, a seventh column is appended to the original 4x6 matrix. Look at the condition that when true, updates the matrix
if nodos(i,5)==0
nodos(i,7)=w+1
w=w+1
end
So if the fifth element of the ith row is zero, then you set the seventh element of the ith row to w+1. But since your matrix is 4x6, a seventh column of zeros is automatically added to the matrix with only nodos(i,7) equal to one. The code then increments w and proceeds to the next iteration of the loop. But the first line of the for loop body
w=0
So even though we have incremented w, we have reset it to zero. I suspect that you only want to initialize w to zero outside of the for loop. So you may want to revise your code as
nodos = [0 0 0 0 1 1; 48 0 0 0 1 1;84 0 0 0 1 1;48 36 -500 0 0 0]
Nnodos = 4;
w = 0;
for i=1:Nnodos
if nodos(i,5)==0
nodos(i,7)=w+1;
w=w+1;
end
end
Now decide what should happen when nodos(i,5)==0 0 do you want to add a new column, or update an existing one with nodos(i,??)=w+1;
  1 件のコメント
Rachuan
Rachuan 2014 年 9 月 4 日
Great, thanks my friend. It was that W inside the loop that was breaking the code.

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

その他の回答 (1 件)

dpb
dpb 2014 年 9 月 4 日
Yes... :) (the answer to the question asked). You can see this as you run the code as given above since the value of w=0 is printed four times, once for each pass thru the loop.
But...the array nodos is modified; you just added a column instead of, perhaps intending to change one of the existing columns, perhaps.
Count 'em...you now at the end have size(nodos)=[4 7] and the last value of the last column is the '1' you added.
The session at my command line follows...
>> nodos = [0 0 0 0 1 1;
48 0 0 0 1 1;
84 0 0 0 1 1;
48 36 -500 0 0 0]
Nnodos=4
for i=1:Nnodos
w=0
if nodos(i,5)==0
nodos(i,7)=w+1
w=w+1
end
end
nodos =
0 0 0 0 1 1
48 0 0 0 1 1
84 0 0 0 1 1
48 36 -500 0 0 0
Nnodos =
4
w =
0
w =
0
w =
0
w =
0
nodos =
0 0 0 0 1 1 0
48 0 0 0 1 1 0
84 0 0 0 1 1 0
48 36 -500 0 0 0 1
w =
1
>>

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by