Print output if elements of a list match values in a column

4 ビュー (過去 30 日間)
Lukas Netzer
Lukas Netzer 2021 年 5 月 3 日
コメント済み: Lukas Netzer 2021 年 5 月 6 日
I have a column/list of values A:
1
2
3
4
5
6
7
8
9
10
And an according list/column of values B:
A
B
C
D
E
F
G
H
I
J
From the column/list of A I am extracting certain values e:
3
6
8
10
Now I have a column C containing values of B in a random order:
A
A
B
F
D
H
C
E
K
C
J
A
And according values in column D:
0.01
0.02
0.12
0.13
0.24
0.09
0.14
0.01
0.04
0.07
0.12
0.19
First step I need to do is match the extracted values e with the values in column A to get the according values from column B to get values f:
C
F
H
J
Second step is to match values in f with values in C and get their according values to g; else it is 0:
0
0
0
0.13
0
0.09
0.14
0
0
0.07
0.12
0
How can I do those steps?
I thought about runnig a loop, but it wont work for me.
Thanks for your help!

採用された回答

Stephen23
Stephen23 2021 年 5 月 3 日
編集済み: Stephen23 2021 年 5 月 3 日
A = [1;2;3;4;5;6;7;8;9;10];
B = ["A";"B";"C";"D";"E";"F";"G";"H";"I";"J"];
e = [3;6;8;10];
C = ["A";"A";"B";"F";"D";"H";"C";"E";"K";"C";"J";"A"];
D = [0.01;0.02;0.12;0.13;0.24;0.09;0.14;0.01;0.04;0.07;0.12;0.19];
[X,Y] = ismember(e,A);
f = B(Y(X))
f = 4×1 string array
"C" "F" "H" "J"
Z = ismember(C,f);
g = zeros(size(C));
g(Z) = D(Z)
g = 12×1
0 0 0 0.1300 0 0.0900 0.1400 0 0 0.0700

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink Coder についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by