the loop does not display results

조회 수: 6 (최근 30일)
Lev Mihailov
Lev Mihailov 2020년 1월 27일
댓글: Lev Mihailov 2020년 1월 27일
Hello! There is a matrix and I need to find the first value less than 90
clear all
close all
X=[ 80 90 100 110 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20]' ;
[~,loc]=max(X); % search j
i=1:length(X(:,1)); % search i
j=loc;
while X(i,j)>90
jx(i)=j(i)+1 % find out how many j are running in iteration 1
end
jx is not shown at all, although the condition is true
while X(i,j)<90
jy(i)=j(i)-1
end
first cycle find out the quantity j while condition X> 90 at each iteration
second cycle find out the quantity j while condition X <90 at each iteration
and the problem I do not see jx and jy
  댓글 수: 2
Adam
Adam 2020년 1월 27일
i and j are vectors so
while X(i,j)
will not do what you want. You can either loop around all the values testing the condition on each or you can use a vectorised approach.
X(i,j) < 90
returns a logical 8x9 array with a 1 (true) in the places that fulfil the condition and 0 otherwise. I'm not really sure what you want jy to output though since as it currently is it doesn't make sense as j is a row vector and i an 8x9 array.
Lev Mihailov
Lev Mihailov 2020년 1월 27일
I do not understand,
% for the first iteration
while X(i,j)>90 % X(1,4) [110 100 95 90 20]
jx(i)=j(i)+1 % here I do not know what to do
% % jx(1)=j(1)+1 % but I need to do while the whole condition is satisfied
% [110 100 95 90 20] %
% jx(1)=j(1)+1
% jx(1)=j(1)+1 this condition for the first iteration must be met twice
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Philippe Lebel
Philippe Lebel 2020년 1월 27일
편집: Philippe Lebel 2020년 1월 27일
You are not looping over the values of "i" nor "j"
i=1:length(X(:,1))
i =
1 2 3 4 5 6 7 8
j =
4 4 4 4 4 4 4 4 4
If you try to evaluate X(i,j), it is equivalent to say that you try to evaluate X([1 2 3 4 5 6 7 8], [4 4 4 4 4 4 4 4 4])
If you want to do what i think you want to do, here is the code:
clear all
close all
X=[ 80 90 100 110 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20
80 90 100 150 100 95 90 20]' ;
[~,loc]=max(X); % search j
j=loc;
for i = 1:length(X(:,1))
for k = 1:length(j)
jx(i)=j(i)+1 % find out how many j are running in iteration 1
if X(i,j(k))>90
break
end
end
if X(i,j(k))>90
break
end
end
this is not optimal but it is easier to understand (i think)
  댓글 수: 3
Philippe Lebel
Philippe Lebel 2020년 1월 27일
this is not relevant to the question asked in this post.
I don't know what is the intent behind your code so i don't know how to respond to this.
Lev Mihailov
Lev Mihailov 2020년 1월 27일
code goal get jx per column.
And the next question is, can a matlab work with its iterations?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by