How can I obtain a certain value from the function 'find' from a 3x2 cell?

I need a certain variable "a" to be equal to either 1,2 or 3 according to two previous selection "Firstselection" and "Secondselection" which algo go from 1 to 3.
I have a 3x2 cell Comb = [1,2; 2,3; 1,3]. I have to assign "a" to a find function so that when Firstselection = 1 & Secondselection = 2 (and viceversa), a = 1, Firstselection = 2 & Secondselection = 3 (and viceversa), a = 2 and Firstselection = 1 & Secondselection = 3 (and viceversa), a = 3, all based on the cell Comb. I already solved it with an if cycle but now I need to apply the function find to the cell to obtain a certain value of "a" according to my 2 selections. I cannot change the cell.

7 commentaires

"I have a 3x2 cell Comb = [1,2; 2,3; 1,3]."
What you show is not a cell array, but a simple numeric matrix.
Do you actually have a cell array (as you write) or a numeric matrix (as you show)?
Sorry, my mistake... Comb is indeed a cell array:
{[1]} {[2]}
{[2]} {[3]}
{[1]} {[3]}
You can convert it o matrix using cell2mat and then use find.
I previously tried that. I know that I can obtain a matrix, but the problem is that I don't know how to apply find to either the cell or the matrix in order to obtain my goal...
It's not clear to me what the desired result is, sorry.
Show us the input and then the expected output...
Firstselection = 1 | 2 | 3; %identified by a numeric slider
Secondselection = 1 | 2 | 3;
Comb = {1,2;2,3;1,3};
Comb = 3×2 cell
% 'a' has to be either 1,2,3 according to my selection as I said previously.
% (Firstselection =1 & Secondselection =2 (and viceversa) -> a = 1, and so on...
% Is there a way to obtain that using the find
% function?
Just repeating the same unclear verbiage doesn't help -- SHOW us the input and corresponding output expected.

Connectez-vous pour commenter.

 Réponse acceptée

I think you're asking how to search for the index of a row in a matrix based on multiple columns (although you have a cell array, you can just convert that to start).
You'll want to think about a few 'special' cases: what if it isn't found? What if it's found in more than one place? ismember does a nice job if you're not concerned with the multiple result issue:
comb={1,2;3,4;1,3};
comb_mat=cell2mat(comb);
target=[1 2];
[~,wherefound]=ismember(target,comb_mat,'rows')
wherefound = 1
target=[3 4];
[~,wherefound]=ismember(target,comb_mat,'rows')
wherefound = 2
target=[1 3];
[~,wherefound]=ismember(target,comb_mat,'rows')
wherefound = 3
target=[12 13];
[~,wherefound]=ismember(target,comb_mat,'rows') % wherefound will be 0 if not found
wherefound = 0
comb={1,2;3,4;1,3;1,2};
comb_mat=cell2mat(comb);
target=[1 2];
[~,wherefound]=ismember(target,comb_mat,'rows') % wherefound will be the first index if multiple occurrences
wherefound = 1
Otherwise you can combine find with an &:
comb={1,2;3,4;1,3};
comb_mat=cell2mat(comb);
target=[1 2];
find(comb_mat(:,1)==target(1) & comb_mat(:,2)==target(2))
ans = 1
target=[3 4];
find(comb_mat(:,1)==target(1) & comb_mat(:,2)==target(2))
ans = 2
target=[1 3];
find(comb_mat(:,1)==target(1) & comb_mat(:,2)==target(2))
ans = 3
target=[12 13];
find(comb_mat(:,1)==target(1) & comb_mat(:,2)==target(2)) % find will return empty if not found
ans = 0×1 empty double column vector
comb={1,2;3,4;1,3;1,2};
comb_mat=cell2mat(comb);
target=[1 2];
find(comb_mat(:,1)==target(1) & comb_mat(:,2)==target(2)) % find will return both indices if multiple results are found
ans = 2×1
1 4

1 commentaire

Thank you very much! This example works great with my code, for the only exception that in my work 'target' is an array that I can decide, so it works just fine.
Thank you again for your time and your help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operators and Elementary Operations dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by