Borrar filtros
Borrar filtros

multiplication of rows by range in a matrix

1 visualización (últimos 30 días)
Berfin Çetinkaya
Berfin Çetinkaya el 22 de Mzo. de 2022
Respondida: Moksh el 29 de Sept. de 2023
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 comentarios
Berfin Çetinkaya
Berfin Çetinkaya el 22 de Mzo. de 2022
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 el 22 de Mzo. de 2022
It's possible, but you stated the rules differently.

Iniciar sesión para comentar.

Respuestas (1)

Moksh
Moksh el 29 de Sept. de 2023
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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by