Retrieving Rows based on Multiple Columns

Suppose,
var1=[ 1 1 3 4 5
2 14 15 16 17];
var2= { 1 5 6 7
3 7 6 5
4 2 5 9
2 1 5 7
5 3 1 6
15 4 9 1
18 5 1 3
17 8 7 4
16 6 6 3
14 2 1 2 };
Based on var1(Column 2,3,4,5), I want to retrieve rows in var2 which corresponds to var1 (Column 2,3,4,5)
Desired Result:
Result{1}
1 5 6 7
3 7 6 5
4 2 5 9
5 3 1 6
Result{2}
15 4 9 1
17 8 7 4
16 6 6 3
14 2 1 2

3 comentarios

Shashank Prasanna
Shashank Prasanna el 3 de Jul. de 2013
Based on your previous question:
Is there a bigger problem you are trying to solve? It would be better if you stated what the whole problem is. Or is this just a learning exercise?
RDG
RDG el 3 de Jul. de 2013
The previous question was structured wrongly, and since editing it would change the meaning entirely, I decided to start a new thread. Others can make use of that for reference.
Guru
Guru el 3 de Jul. de 2013
Editada: Guru el 3 de Jul. de 2013
Ok, this is rather confusing, and maybe I have to refer to the question that Shashank referred.
Your desired results however look like Result{1} should be the rows 1,2,3,5 of var2.
However your var1 columns 2,3,4,5 should be 1,3,4,5. Is this a typo on your part?
Then Result{2} are rows 6,8,9,10 of var2 which really doesn't correspond at all to var1 data...
Please clarify what you are trying to do better.

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 3 de Jul. de 2013
Editada: Azzi Abdelmalek el 3 de Jul. de 2013
c=cell2mat(var2(:,1));
idx=arrayfun(@(x) find(ismember(c,x))',var1(:,[2 3 4 5]),'un',0)
idx1=arrayfun(@(x) cell2mat(idx(x,:))',[1:size(var1,1)]','un',0)
result=cellfun(@(x) var2(x,:),idx1,'un',0)
result{1}
result{2}

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

RDG
el 3 de Jul. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by