How to use if statement inside a for loop?

1 回表示 (過去 30 日間)
Emily
Emily 2022 年 5 月 5 日
コメント済み: Walter Roberson 2022 年 5 月 6 日
I'm currently trying to print out something if the difference between the plots is bigger than 100.
for i = 2:10
plot(x(:,25+i)
dif(:,i) = x(:,25+i) - x(:,25+(i-1)); %error here
if dif >200
fprintf( ) %print out the x value where it was triggered
else
fprintf('nothing')
end
I'm having trouble saving each occurance that it happens.

採用された回答

Walter Roberson
Walter Roberson 2022 年 5 月 5 日
for i = 2:10
plot(x(:,25+i)) ; hold on
dif(:,i) = x(: ,25+i) - x(:,25+(i-1));
mask = dif(:, i) > 200
if any(mask)
fprintf('col %d: %s\n', 25+i, mat2str(x(mask, 25+i))); %print out the x value where it was triggered
else
fprintf('col %d: nothing\n');
end
end
  4 件のコメント
Emily
Emily 2022 年 5 月 6 日
Size of x is about 2000x2000 value from a struct, and there is a difference between the some of the figures but depends on the input file number.
I was able get it working by getting rid of the (:,i) and only using dif=. But now not sure how to display the X-axis value on where this is happening as the code prints out the Y-axis value of the looped graphs when mask is detected.
Walter Roberson
Walter Roberson 2022 年 5 月 6 日
The code I showed cannot print out the y axis values. The code I showed uses mat2str(x(mask, 25+i)) which displays x values, not y values.
The vectorized form of the code would look more like
i = 2:10;
plot(x(25+i))
dif = x(:,25+i) - x(:,25+i-1);
mask = dif > 200;
for c = 1:size(dif,2)
submask = mask(:,c);
if any(submask)
fprintf('col %d: %s\n', 25+c+1, mat2str(x(submask, 25+c+1))); %print out the x value where it was triggered
else
fprintf('col %d: nothing\n', 25+c+1);
end
end

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by