multiplication by taking into account the values when creating a matrix

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 27 Mag 2022
Modificato: Stephen23 il 27 Mag 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).

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 27 Mag 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
  3 Commenti
Stephen23
Stephen23 il 27 Mag 2022
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

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by