multiplication of rows by range in a matrix

조회 수: 5 (최근 30일)
Berfin Çetinkaya
Berfin Çetinkaya 2022년 3월 22일
답변: Moksh 2023년 9월 29일
i have two matrices
matrix A:
50 44 80 0
50 0 80 52
50 0 80 52
49 49 2 2
matrix B:
5 4 9 7
10 3 10 6
If 1,2,3,4 or 5 is written in matrix b, multiply the last row in matrix A with the first row.
If matrix b says 6 or 7, multiply the last row in matrix A with the second row.
If 8 is written in matrix b, multiply the last row in matrix A with the third row.
However, let him continue this by examining both columns separately. It should return for each row of matrix B.And display them in a new matrix
For example the new matrix:
2450 2156 160 104
2450 132 160 104
Is such a code possible?
Thank u for help
  댓글 수: 3
Berfin Çetinkaya
Berfin Çetinkaya 2022년 3월 22일
True, 132 come from A(1,2)*B(2,2) = 44*3 = 132.
These matrices relate to a real-life problem. So there is no way to multiply these two matrices?
Torsten
Torsten 2022년 3월 22일
It's possible, but you stated the rules differently.

댓글을 달려면 로그인하십시오.

답변 (1개)

Moksh
Moksh 2023년 9월 29일
Hi Berfin,
I understand that you have defined two matrices “A” and “B” and defined some multiplication rules to generate a new matrix.
You can try using a “for-loop” with matrix indexing in MATLAB to iterate over the elements of “B” for generating the new resulting matrix.
Since the multiplication rule for “9” and “10” are not specified, I am assuming that the rules for these two are same as when the encountered element is “8”.
Kindly refer to the following code snippet, to perform these operations:
% A and B matrices
A = [50 44 80 0; 50 0 80 52; 50 0 80 52; 49 49 2 2];
B = [5 4 9 7; 10 3 10 6];
% Resulting new matrix
res = zeros(size(B));
% Sizes of A and B
szB = size(B);
szA = size(A);
for i = 1 : szB(1)
for j = 1 : szB(2)
row1 = -1;
row2 = -1;
if B(i, j) >= 1 && B(i, j) <= 5
row1 = 1;
row2 = szA(end);
end
if B(i, j) >= 6 && B(i, j) <= 7
row1 = 2;
row2 = szA(end);
end
if B(i, j) >= 8 && B(i, j) <= 10
row1 = 3;
row2 = szA(end);
end
row_res = A(row1, :) .* A(row2, :);
res(i, j) = row_res(j);
end
end
disp(res)
2450 2156 160 104 2450 2156 160 104
For more information about indexing arrays and “for-loops”, please refer to the following documentation:
Hope this helps resolve the problem.
Best Regards,
Moksh Aggarwal

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by