How to filter rows?

조회 수: 48 (최근 30일)
Ammy
Ammy 2018년 2월 9일
댓글: Les Beckham 2023년 2월 22일
I have a matrix like this
1 2 3 4 5 6 7 8
2 3 4 1 2 3 4 5
1 2 3 4 1 1 1 1
1 2 3 5 4 2 2 1
I want to separate those rows having first four entries as 1 2 3 4, as in above case I separate out row 1 and row three.

채택된 답변

Aletta Wilbrink
Aletta Wilbrink 2018년 2월 9일
편집: Aletta Wilbrink 2018년 2월 9일
Probably not the most efficient way, but this works
b = a(a(:,1)==1 & a(:,2) == 2 & a(:,3) == 3 & a(:,4) == 4,:)
Where a is the name of your matrix
  댓글 수: 2
Ammy
Ammy 2018년 2월 9일
Thank you very much.
Aletta Wilbrink
Aletta Wilbrink 2018년 2월 9일
편집: Aletta Wilbrink 2018년 2월 9일
Seeing Guillaume's answer, a better way is
b = a(all(a(:,1:4) == [1 2 3 4],2),:)

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

추가 답변 (1개)

Guillaume
Guillaume 2018년 2월 9일
%R2016b or later:
tokeep = all(A(:, 1:4) == [1 2 3 4], 2)
%earlier versions
tokeep = all(bsxfun(@eq, A(:, 1:4), [1 2 3 4]), 2)
%then
A(tokeep, :)
  댓글 수: 4
Kurt
Kurt 2023년 2월 22일
The question is simply "How to filter rows?"
Anyway, I found the answer I needed. To filter rows of cells (not numerics) you can either read your data in as a table or convert your matrix to a table. Then use this one-liner:
output_data = input_data(table2array(input_data(:,col)) == "pattern",:);
where "col" is the column number you are filtering on.
There may be other approaches, but this works for me.
Les Beckham
Les Beckham 2023년 2월 22일
I'm glad you figured it out.

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

카테고리

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