what is the mistake,
Mostrar comentarios más antiguos
Given two real square matrix of order n. Get a new matrix of the elements of each column of the first matrix times elements of the corresponding rows of the second matrix. i coded using matlab but unfortunately it applys only for (3x3)matrix.what should i do so that it may apply for every kind of matrix, for example(4x4),(5x5) and more.
codes
clc
clear
a=[1 2 3;4 5 6;7 8 9];
b=[1 2 3;4 5 6;7 8 9];
for i=1:length(a)
for j=1:length(b)
c(i,j)=a(i,j)*b(j,i)
end
end
disp(a);
disp(b);
disp(c);
1 comentario
From what you wrote I can't deduce how you want to get the new matrix from the two given matrices.
Your code from above just calculates C as
C=A.*B.'
I guess this is not what you have in mind.
Best wishes
Torsten.
Respuesta aceptada
Más respuestas (2)
Roger Stafford
el 23 de Mayo de 2016
If A and B are your two square matrices
C = A.*(B.');
Guillaume
el 23 de Mayo de 2016
0 votos
"unfortunately it applys only for (3x3)." The code you've posted applies to any size of matrix. There's is nothing restricting it to 3x3 matrices, so it's not clear why you're asking the question.
Note that you don't need a loop to perform the operation you're carrying out. Have a look at memberwise multiplication .* and transpose .'.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!