multiplication by taking into account the values when creating a matrix

1 回表示 (過去 30 日間)
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 5 月 26 日
コメント済み: Berfin Çetinkaya 2022 年 5 月 27 日
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 件のコメント
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 5 月 26 日
Thank u for hit.
Good work.
Stephen23
Stephen23 2022 年 5 月 27 日
編集済み: Stephen23 2022 年 5 月 27 日
@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
Stephen23 2022 年 5 月 27 日
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 件のコメント
Stephen23
Stephen23 2022 年 5 月 27 日
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
Berfin Çetinkaya
Berfin Çetinkaya 2022 年 5 月 27 日
Thank you to you. You have been of great help.
Good work !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by