multiply values in different rows

조회 수: 2 (최근 30일)
Berfin Çetinkaya
Berfin Çetinkaya 2022년 5월 25일
댓글: DGM 2022년 5월 25일
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.

채택된 답변

DGM
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,:))
output = 3×4
10.0000 10.0000 2.0000 3.0000 5.0000 8.0000 1.5000 2.0000 7.5000 8.0000 0.5000 2.0000
  댓글 수: 2
Berfin Çetinkaya
Berfin Çetinkaya 2022년 5월 25일
True answer .Thank you.I have one more question.
For example, since 2 is output in matrix A, it takes the value in the second row in matrix B. Is it okay if it takes the second value in the C matrix too? Then it has to multiply these two values.
Example :
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
new matrice :
0,5*20 0,4*15 0,1*10 0,2*5
0,5*20 0,4*15 0,1*10 0,2*5
0,5*20 0,4*15 0,1*10 0,2*5
0,5*20 0,4*15 0,1*10 0,2*5
new matrice:
10 6 1 1
10 6 1 1
10 6 1 1
10 6 1 1
Thank you for help!
DGM
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])
output = 4×4
10 6 1 1 10 6 1 1 10 6 1 1 10 6 1 1

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by