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

1 view (last 30 days)
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 Comments
dpb
dpb on 22 Oct 2021
Just repeating the same unclear verbiage doesn't help -- SHOW us the input and corresponding output expected.

Sign in to comment.

Accepted Answer

Dave B
Dave B on 21 Oct 2021
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 Comment
Pietro Fedrizzi
Pietro Fedrizzi on 22 Oct 2021
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!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by