MATLAB Answers

0

How can I solve if condition in for loop matrix?

Dina Maulina さんによって質問されました 2019 年 5 月 30 日
最新アクティビティ Dina Maulina さんによって コメントされました 2019 年 6 月 28 日
I want to make an iteration that when the component of a matrix A has less value than the component of matrix B, it will continue the process to the next iteration.
A =
[ 2 5 9 12
6 4 13 1
3 2 19 5]
B=
[ 5
5
5]
in the first column, the condition is not met so it cannot proceed to the second column. and in the second column, conditions have been met so that it can proceed to the third column.
I tried in for loop but, the A values always stuck in first column even condition is right.
Thank you.

  3 件のコメント

madhan ravi
2019 年 5 月 30 日
What is your desired result? Show it explicitly.
Dina Maulina 2019 年 5 月 30 日
What I want is when the all of the A component value <= B, then the loop will continue to the next column.
Guillaume
2019 年 5 月 30 日
then the loop
What loop? Remember that we have no idea what you're doing. If you don't show us your code, how can we tell us how to fix it?
Most likely, whatever loop you're using is not even necessary.

サインイン to comment.

2 件の回答

Murugan C
回答者: Murugan C
2019 年 5 月 30 日
編集済み: Guillaume
2019 年 5 月 30 日
 採用された回答

Hi
use below code for your query.
Here, B will check all rows, each column in A is less than, then it will allow to next iteration.
A = [ 2 5 9 12; 6 4 13 1; 3 2 19 5]
B = [5; 5 ;5];
[row,col] = size(A);
for ii = 1 : col
% fprintf('Current Column is : %d\n', ii);
C1 = A(:,ii) < B;
if all(C1,1)
fprintf('Current Column is : %d, Next Column is : %d\n', ii, ii+1);
continue;
else
fprintf('Stopped at %d Column because unsatisfied B input \n', ii);
break;
end
end

  3 件のコメント

Guillaume
2019 年 5 月 30 日
Please use the toolbar to format your answer properly. I've done it for you this time.
Note that your continue does nothing useful since the loop will continue on its own anyway. It would be better if it weren't there (otherwise a reader may wonder if that pointless continue is an indication of a bug).
Murugan C
2019 年 5 月 30 日
Thanks Guillaume.
I knew, continue will not take any action in 'for' loop. Any how I will try avoid it..
Dina Maulina 2019 年 6 月 28 日
Thank you so much for your help.

サインイン to comment.


KSSV
回答者: KSSV
2019 年 5 月 30 日

all(A<B,2)

  0 件のコメント

サインイン to comment.



Translated by