multiply values in different rows
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi!
I have three matrices.
first matrices :
A=
2 3 1 4
8 5 2 3
1 2 6 7
6 8 4 1
B=
0,1
0,5
0,4
0,2
C=
10
20
15
5
25
15
10
20
For example, look at the value in the first matrix for the cell that says 8 in matrix A. For example, it says 2 on top of 8. Since it writes 2, let it take the value of the 2nd row in B matrix. (A value of 0.5 is taken.)
Since 8 is written in matrix A, take the value written in 8th row from matrix C. (It takes the value 20.)
And multiply the received value of 0.5 by the value of 20.
Let it do this for all cells in matrix A.
new matrices=
10 10 2 3
5 8 1,5 2
7,5 8 0,5 2
Thank you for help.
0 Kommentare
Akzeptierte Antwort
DGM
am 25 Mai 2022
I'm going to guess that this is what you're after:
A = [2 3 1 4; 8 5 2 3; 1 2 6 7; 6 8 4 1];
B = [0.1; 0.5; 0.4; 0.2];
C = [10; 20; 15; 5; 25; 15; 10; 20];
output = B(A(1,:)).' .* C(A(2:end,:))
2 Kommentare
DGM
am 25 Mai 2022
I'm not sure what determines the number of output rows, but assuming it's 4:
A = [2 3 1 4; 8 5 2 3; 1 2 6 7; 6 8 4 1];
B = [0.1; 0.5; 0.4; 0.2];
C = [10; 20; 15; 5; 25; 15; 10; 20];
output = B(A(1,:)).*C(A(1,:));
output = repmat(output.',[4 1])
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!