using nested for loops

1 vue (au cours des 30 derniers jours)
Durgga Rajendren
Durgga Rajendren le 18 Avr 2011
Commenté : Leon Barainsky le 17 Déc 2019
Hi, how to use nested for loops to multiply 2 matrices and make it work just like MATLAB operator? The function must work on matrices of any compatible size. I know what is nested for loops but in this case ,I dunno hw to apply it. Any help is much appreciated.Thanks.

Réponses (1)

Andrei Bobrov
Andrei Bobrov le 18 Avr 2011
most unwise variant
function C = yourmtimes(A,B)
[m,namb] = size(A);
n = size(B,2);
C = zeros(m,n);
for ii = 1:m
for jj = 1:n
c1 = 0;
for k = 1:namb
c1 = c1 + A(ii,k)*B(k,jj);
end
C(ii,jj) = c1;
end
end
more
m = size(A,1);
n = size(B,2);
C = zeros(m,n);
for ii = 1:m
for jj = 1:n
C(ii,jj) = sum(A(ii,:).*B(:,jj).');
end
end
or
m = size(A,1);
n = size(B,2);
C = zeros(m,n);
for ii = 1:m
for jj = 1:n
C(ii,jj) = A(ii,:)*B(:,jj);
end
end
or
m = size(A,1);
n = size(B,2);
C = zeros(m,n);
nones = ones(n,1);
for ii = 1:m
C(ii,:) = sum(A(ii*nones,:).'.*B);
end
etc.
  1 commentaire
Leon Barainsky
Leon Barainsky le 17 Déc 2019
None of them work for me, could there be something wrong with my matlab settings?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by