Converting a the result vector from logical indexing into a matrix possible or unnescesary?

조회 수: 1 (최근 30일)
Hello everyone,
I need to extract rows of a matrix with a certain value in one column. At the moment I am using logical indexing i this way:
c=A(:,2)==3; T=A([c,c,c,c]);
The problem I have is that T is a vector, with the columns added end to end, but I need it as a matrix.
Does Matlab provide an easy way to convert such a vector to a matrix or is there another easy way to extract the rows into a matrix?
Thank you everybody.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 12월 4일
T = A(A(:,2) == 3,:);

추가 답변 (1개)

Jos (10584)
Jos (10584) 2013년 12월 7일
Often, you get some insight when you remove the semi-colons
A = [1 2 3 4 ; 11 3 13 14 ;21 22 23 24 ; 41 3 43 44] % example data
c = A(:,2)==3
T = A(c,:)
if T is empty, there are simply no rows that have a value of 3 in the second column.
  댓글 수: 1
Daniel
Daniel 2013년 12월 7일
Sorry was using the wrong matrix. The first answer works just fine. To much data and I lost track of what is what. Thanks to both of you.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by