About "ismember" function
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Bruno Luong
2022-3-1
编辑:Bruno Luong
2022-3-1
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 个评论
更多回答(2 个)
Simon Chan
2022-3-1
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 个评论
Arif Hoq
2022-3-1
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!