# multiplication by taking into account the values when creating a matrix

3 views (last 30 days)
Berfin Çetinkaya on 26 May 2022
Commented: Berfin Çetinkaya on 27 May 2022
A matrix:
A = [2 1 4 3; ...
10 15 20 10; ...
20 10 15 20]
B matrix:
B = [1 3 4; ...
4 5 6; ...
2 1 4; ...
3 5 1]
C matrix:
C = [50;100;25;10]
I need to create a new matrix. I will use the values in matrices A, B and C when creating this matrix.I will try to explain how I want to create my new matrix through an example.
The matrix we want to create will be a 2x4 matrix.
Values in matrix A are important. First of all, the values in the first column are checked. The value of A(1,1) is 2. so the second row values in B and C are multiplied. but in B there are three columns in the second row. We must choose one of the three columns. When choosing, we consider the values in the second and third rows of A. If it is 10, the first column in B matrix is multiplied by B(2,1). If it is 15, the second column in B matrix is multiplied by B(2,2). if it is 20, the third column in B matrix is multiplied by B(2,3).
New matrix :
[400 150 10 75;
25 50 50 100]
What code is required to create such a matrix?
Thanks for help !
Stephen23 on 27 May 2022
Edited: Stephen23 on 27 May 2022
@Berfin Çetinkaya: please explain the output matrix in these two locations:
• you wrote out(1,4)=75, but
• A(2,4)=10 -> 1st column of B
• A(1,4)=3 -> 3rd row of B & C
• B(3,1)=2
• C(3)=25
• 2*25 -> out(1,4)=50
and also:
• you wrote out(2,1)=25, but
• A(3,1)=20 -> 3rd column of B
• A(1,1)=2 -> 2nd row of B & C
• B(2,3)=6
• C(2)=100
• 6*100 -> out(2,1)=600
In the latter case out(2,1)=25 seems an unlikely output value given that C(2)=100 (later multiplied by an integer).

Stephen23 on 27 May 2022
A = [2,1,4,3;10,15,20,10;20,10,15,20]
A = 3×4
2 1 4 3 10 15 20 10 20 10 15 20
B = [1,3,4;4,5,6;2,1,4;3,5,1]
B = 4×3
1 3 4 4 5 6 2 1 4 3 5 1
C = [50;100;25;10]
C = 4×1
50 100 25 10
lkp = [10,15,20];
idx = A(1,:);
[~,idc] = ismember(A(2:end,:),lkp);
szi = size(idc);
[~,idr] = ndgrid(1:szi(1),idx);
idy = sub2ind(size(B),idr,idc);
out = B(idy) .* C(idx,:).'
out = 2×4
400 150 10 50 600 50 50 100
Berfin Çetinkaya on 27 May 2022
Thank you to you. You have been of great help.
Good work !

### Categories

Find more on Shifting and Sorting Matrices 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!