I have a matrix and I am actually interested in obtaining a vector that will consist of the first column in each row that has values lower certain value.

조회 수: 1 (최근 30일)
I have a matrix and I am actually interested in obtaining a vector that will consist of the first column in each row that has values lower 3. e.g if I have A=[ 5 7 8 3 2 ;6 2 1 4 4;1 2 3 4 5; 4 5 2 1 3]. I expect an Ans: [5;2;1;4]

채택된 답변

Saint
Saint 2017년 5월 29일
편집: Saint 2017년 5월 29일
Thanks Star Strider and MathsReallyWork. I kind of figure it out.Ans: [5;2;1;3]
dmax=3
Nrows=4
A=[5 7 8 3 2 ;6 2 1 4 4;1 2 3 4 5; 4 5 2 1 3]
for i=1:Nrows
B=A(i,:)';
a(i)=find(B<dmax,1,'first') ;
end
b=a'
  댓글 수: 1
Stephen23
Stephen23 2017년 5월 29일
편집: Stephen23 2017년 5월 29일
It is simpler and more efficient to process the whole array at once, rather than using a loop:
>> C = A.';
>> idx = C<dmax;
>> [col,row] = find(idx & cumsum(idx,1)==1)
col =
5
2
1
3
row =
1
2
3
4

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

추가 답변 (1개)

MathReallyWorks
MathReallyWorks 2017년 5월 27일
Hello saint,
Your question is not clear. Please edit it.
By the description of your question I can guess that you want first column of each row provided that entry is less than 3. In that case this code works well:
A = [ 5 7 8 3 2 ;6 2 1 4 4;1 2 3 4 5; 4 5 2 1 3];
newA = A(A(:,1)<3,1)
But, then you said you are expecting [5;2;1;4] which is contradictory to your question.
[5;6;1;4] is possible as it contains all first element.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by