multiply values in different rows

5 views (last 30 days)
Berfin Çetinkaya
Berfin Çetinkaya on 25 May 2022
Commented: DGM on 25 May 2022
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.

Accepted Answer

DGM
DGM on 25 May 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,:))
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 Comments
Berfin Çetinkaya
Berfin Çetinkaya on 25 May 2022
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 on 25 May 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])
output = 4×4
10 6 1 1 10 6 1 1 10 6 1 1 10 6 1 1

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!