multiplication of rows by range in a matrix
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (1 件)
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)
For more information about indexing arrays and “for-loops”, please refer to the following documentation:
- indexing: https://in.mathworks.com/help/matlab/math/array-indexing.html
- for-loops: https://in.mathworks.com/help/matlab/ref/for.html
Hope this helps resolve the problem.
Best Regards,
Moksh Aggarwal
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!