multiply values in different rows
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
DGM
2022-5-25
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 个评论
DGM
2022-5-25
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])
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!