Question regarding a find function(search for data)

Hi, I have the following matrix:
data=[10 1 2 3; 11 4 5 6; 12 7 8 9; 17 4 5 6]
I want to do a query with the following matirx:
built a matrix base on the element value 10 and 17
lookfor=[10;17]
And I'm looking for the following result(values associate with 10 and 17 in this case)
anwser=[10 1 2 3; 17 4 5 6]
I'm looking for a dynamic anwser, data matrix and lookfor matrix will vary and be much more bigger.
thank you in advance for your precious anwsers.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 26일
편집: Azzi Abdelmalek 2013년 5월 26일

0 개 추천

out=[data(any(data==17,2),:);data(any(data==10,2),:)]
%or
out=data(any(ismember(data,lookfor),2),:)

댓글 수: 4

Gimpy
Gimpy 2013년 5월 26일
is there a way to do a "group by" with this method?
let'S say the data is the following: data=[10 1 2 3; 11 4 5 6;10 0 20 30; 11 4 5 6; 12 7 8 9; 17 40 50 60]
lookfor=[10;17]
And I want the following anwser:
anwser=[10 1 22 33; 11 8 10 12]
thank you
Gimpy
Gimpy 2013년 5월 26일
using: out=data(any(ismember(data,lookfor),2),:)
How did you find anwser=[10 1 22 33; 11 8 10 12]
Looks like he wants to find rows that start with 10, and sum them into one row. Then find rows that start with 11, and sum them.

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

추가 답변 (1개)

the cyclist
the cyclist 2013년 5월 26일
편집: the cyclist 2013년 5월 26일

0 개 추천

rowIndex = ismember(data(:,1),lookfor);
answer = data(rowIndex,:);

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by