About "ismember" function
조회 수: 7 (최근 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!