필터 지우기
필터 지우기

how to write a function that multiplies any 2 matrices that are of compatible size by using nested for loop?

조회 수: 1 (최근 30일)
For example not just a (2X2)matrix, but also for a (3X1) matrix and (3X3) matrix. I'm confused by a nested loop ? How do you nest it anyway? Thanks I am new to matlab

답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2015년 11월 27일
You simply do something along these lines:
for i1 = 1:size(M1,1)
for i2 = 1:size(M2,2),
Res(i1,i2) = M1(i1,i2) + M2(i1,i2); % Or whatever operator you're interested in
end
end
Above I've not bothered checking that this is the proper ordering of the indexing for your desired multiplication of matrices - since the * operator in matlab is intended to do matrix multiplication for you, I guess this is for the learning experience...
You should also pre-allocate the Res array (Res = zeros(sy,sx);) to avoid growing it incrementally which wastes lot of time.
HTH

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by