Compare elements of cell array

HI I have a 2D array like this:
and a cell array:
I want to search elements of cell array in 1st column of 2D array. Like 2nd row of cell array has 4 and 3, it is present in 1st column of 2D array, it should display both rows (and all other elements as zero).
Thanks in advance.

5 Commenti

What specifically do you want the output to look like for this example?
Also, it is much easier for us if you paste in code that generates the matrices, or upload a file. If you only upload an image, we have to code that ourselves.
I think I understand, but I have two more clarifying questions:
Do you want the output to be a 6x1 cell array (for this example), in which each cell contains a numeric array?
For the cell for the 2nd row, should output be the full 7x7 array (with some rows zeroed out), or just the 2x7 array of the corresponding rows that are indicated?
Similarly, for the 1st row, do you want to have an empty array, or a 7x7 array of all zeros?
For first question answer is Yes. For second question, If it is a 2x7 array, it will be best because I need only those two rows for further calculation and similarly for 1st row it can be an empty array.
Stephen23
Stephen23 il 2 Apr 2017
Modificato: Stephen23 il 2 Apr 2017
@Tha saliem: I notice that you are clearly using my code from your earlier question:
Is there a reason why you do not accept my answer... even though you are using my answer?
Also it would be much easier if you actually told us what you are trying to achieve, rather than trying to solve these small tasks. Then we could help you to develop a solution for your main task, rather then waste time trying to figure out solutions (that you do not accept) to every small task:
Im really sorry I was using it through cell phone and i clicked on Accept answer at the time when i commented on that. I didn't even know that it was not accepted. Sorry for this. and thanks for your code it really helped. The main task is quite large and i am working on it from 4 to 5 weeks so it will be difficult for me to sum it up.

Accedi per commentare.

 Risposta accettata

M = [0 1
1 0
2 1
3 0
4 1
5 0
6 0];
C = {[];[4;3];[3;1;4];2;[1;3;2;4];5;5};
output = cellfun(@(x)M(sort(x),:),C,'UniformOutput',false);

2 Commenti

Thank you so much. But this is comparing based on indexes, I want to compare it based on values of first column.
Problem solved. Thanks alot for your help.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by