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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Pietro Fedrizzi
Pietro Fedrizzi il 21 Ott 2021
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?
dpb
dpb il 22 Ott 2021
Just repeating the same unclear verbiage doesn't help -- SHOW us the input and corresponding output expected.

Accedi per commentare.

Risposta accettata

Dave B
Dave B il 21 Ott 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 Commento
Pietro Fedrizzi
Pietro Fedrizzi il 22 Ott 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!

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