About "ismember" function
8 views (last 30 days)
Show older comments
How can I compute the answer for this example?
For example, I have two Excel files:
First Exel File -> PD
Var1 Var2
AAA 1
AA 2
A 3
BBB 5
BB 7
B 13
Second Excel File -> Ui
Var1 Var2 Var3 Var4
B 2 3 5
A 4 6 8
BB 6 7 1
B 2 1 6
A 2 8 1
AA 10 8 1
B 9 2 10
What I need to do is compare the values of Ui to the same rating value of PD. If the Value of Ui is greater than PD's value, it will be 1. Then, apply this method for each Var (Var2, Var3, and Var4).
For example, the first value of Ui, which is 2, must be compared with 13 which is a B rating in PD. And the output has to be 0 because Ui is smaller than PD's value.
Therefore, the output should be:
0 0 0
1 1 1
0 1 0
0 0 0
0 1 0
1 1 0
0 0 0
How can I do this with MatLab?
I asked similar thing before, but having error "Error using categorical/ismember (line 69)."
Thank you for your attention :)
0 Comments
Accepted Answer
Bruno Luong
on 1 Mar 2022
Edited: Bruno Luong
on 1 Mar 2022
No loop or arrayfun is needed
Var1=["AAA" "AA" "A" "BBB" "BB" "B"]';
Var2=[1 2 3 5 7 13]';
PD=table(Var1,Var2)
Var1=["B" "A" "BB" "B" "A" "AA" "B"]';
Var2=[2 4 6 2 2 10 9]';
Var3=[3 6 7 1 8 8 2]';
Var4=[5 8 1 6 1 1 10]';
UI=table(Var1,Var2,Var3,Var4)
[tf,loc]=ismember(UI(:,1),PD(:,1));
table2array(UI(tf,2:end))>=table2array(PD(loc(tf),2))
0 Comments
More Answers (2)
Simon Chan
on 1 Mar 2022
If you would like to use function ismember, then you may try the following:
value = arrayfun(@(x) PD.Var2(ismember(PD.Var1,x)),Ui.Var1); % Value to be compared
index = table2array(Ui(:,2:4))>=value; % Your output index
0 Comments
Arif Hoq
on 1 Mar 2022
try this:
var1={'AAA' 1;'AA' 2;'A' 3;'BBB' 5;'BB' 7;'B' 13};
var2={'B' 2 3 5;'A' 4 6 8;'BB' 6 7 1;'B' 2 1 6;'A' 2 8 1;'AA' 10 8 1;'B' 9 2 10};
C=cell(1,7);
str={'B','A','BB','B','A','AA','B'}
for n=1:7
idx(n)=find(ismember(var1(:,1),str(n)));
C{1,n}=cell2mat(var2(n,2:end))>=cell2mat(var1(idx(n),2));
end
matrix=[C{:}];
output=reshape(matrix,3,7)'
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!