Hello there, I have a short question, as for some reason a for loop doesn't function.

1 回表示 (過去 30 日間)
Soabon
Soabon 2017 年 10 月 23 日
コメント済み: Soabon 2017 年 10 月 26 日
RT is a matrix and krit_out1 is a vector. There is no error message, but the loop is stuck in the first row of the matrix and I don't find the reason.. Can someone help me?
for i = 1:50
RT(RT(:,i)> krit_out1(i)) = NaN;
end
  1 件のコメント
Stephen23
Stephen23 2017 年 10 月 24 日
Loops are not required to solve this. See Jan Simon's answer for a simple and efficient solution.

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

採用された回答

Birdman
Birdman 2017 年 10 月 23 日
RT=ones(50,50);krit_out1=zeros(50,1);
for i=1:50
for j=1:50
if(RT(j,i)>krit_out1(j))
RT(j,i)=NaN;
end
end
end
Try this.
  2 件のコメント
Jan
Jan 2017 年 10 月 23 日
krit_out1( i ) instead of j.
The vectorization of such loops is not only nice and processed efficiently, but without indices, there are less chances for typos.
Soabon
Soabon 2017 年 10 月 24 日
Yes, with the correction this is what I needed (and it works with MATLAB 2015b). Thanx!

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

その他の回答 (2 件)

Jan
Jan 2017 年 10 月 23 日
編集済み: Jan 2017 年 10 月 24 日
Or without a loop:
RT(RT > krit_out1(:).') = NaN; % >= R2016b: Auto-Expanding
For older Matlab versions:
index = bsxfun(@gt, RT, krit_out1(:).'); % [EDITED]
RT(index) = NaN;
  4 件のコメント
Soabon
Soabon 2017 年 10 月 26 日
編集済み: Soabon 2017 年 10 月 26 日
The size of RT is 51 x 50 and the length of krit_out1 is 50. Does that make a difference for the solution?
Soabon
Soabon 2017 年 10 月 26 日
Now this solution also worked well.

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


Ray
Ray 2017 年 10 月 23 日
Try the following. It looks like you intend to operate on the ith column using the ith element of a vector called krit_out1:
for i = 1:50
RT(RT(:,i)> krit_out1(i) ,i) = NaN;
end
  4 件のコメント
Jan
Jan 2017 年 10 月 24 日
編集済み: Jan 2017 年 10 月 24 日
Ray's code uses only the same indices as the code you have posted. Therefore this piece of code cannot produce the error message, except if you are using a different loop counter, but insert "i".
This method is better than using another loop.
Soabon
Soabon 2017 年 10 月 26 日
You're right, I applied the code a second time, now it works. I don't know why there was the error message before.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by