multiplication by taking into account the values when creating a matrix

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 !

3 Comments

Again the hint: Please post the data such, that the readers can use them by copy&paste to create an answer. It is useful to make it as easy as possible to suggest a solution.
@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).

Sign in to comment.

 Accepted Answer

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

3 Comments

Thanks!
Well, if it is equal to 10, 15 and 20, we have them done.
if the condition were : If it is equal to 10 and 11, multiply by the value in the first column.
If it's equal to 12 and 13, multiply by the value in the second column.
If it's equal to 13 and 14, multiply by the value in the third column.
i.e. if it gives us a range for equality
What code is required for this?
You describe placing values into bins, i.e. binning , which can be efficiently achieved using HISTCOUNTS or DISCRETIZE or similar. For example (the changed lines are commented):
A = [2,1,4,3;11,16,20,10;25,10,15,21] % values on and between bin edges
A = 3×4
2 1 4 3 11 16 20 10 25 10 15 21
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,25]; % bin edges
idx = A(1,:);
idc = discretize(A(2:end,:),lkp) % place values of A into the bins
idc = 2×4
1 2 3 1 3 1 2 3
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
Thank you to you. You have been of great help.
Good work !

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!