Try this: x = randi(9, 700, 900); % Sample data. y = randi(9, 900, 1100); [rowsx, colsx] = size(x); [rowsy, colsy] = size(y); theMatrixProduct = zeros(rowsx, colsy); for row = 1 : rowsx row % Print progress to command window. for col = 1 : colsy theSum = 0; for k = 1 : colsx theSum = theSum + x(row, k) * y(k, col); end theMatrixProduct(row, col) = theSum; end end Answer from Image Analyst on mathworks.com
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 303232-how-to-multiplying-matrices-using-for-loops
How to Multiplying matrices using for loops - MATLAB Answers - MATLAB Central
September 16, 2016 - https://www.mathworks.com/matlabcentral/answers/303232-how-to-multiplying-matrices-using-for-loops#answer_234938 ... A.*B is not defined between matrices of difference sizes. It is not possible to write code for it. The .* (otherwise known as times) operator is for element-by-element multiplication. If you are looking for algebraic matrix multiplication then the operator to read about is * (otherwise known as mtimes)
Top answer
1 of 3
3
s1 = size(A); s2 = size(B,2); out=zeros(s1(1),s2); for ii = 1:s1(1) for jj = 1:s2 p=0; for k = 1:s1(2) p = p + A(ii,k)*B(k,jj); end out(ii,jj)=p; end end
2 of 3
2
Well, you do show some code. Lets see how we can write it to help you out. First, understand that a matrix multiply is just a set of dot products. So we can define the (ii,jj) element of the result as just C(ii,jj) = dot(A(ii,:),B(:,jj)) Or, I could write it as: C(ii,jj) = A(ii,:)*B(:,jj); That too is a dot product, between the corresponding vectors. Unfortunately, we can't just leave it like that, as MATLAB won't understand what we are writing. We need to put loops around it, defining ii and jj using for loops. [ra,ca] = size(A); [rb,cb] = size(B); C = zeros(ra,cb); for ii= 1:ra for jj = 1:cb C(ii,jj) = dot(A(ii,:),B(:,jj)); end end Note that I preallocated C. That was important. Anyway, written as loops around a dot product, the above code will work. But really, this requires a triple set of loops, because the dot product itself can/should/might be expanded. [ra,ca] = size(A); [rb,cb] = size(B); C = zeros(ra,cb); for ii= 1:ra for jj = 1:cb % expanded: C(ii,jj) = dot(A(ii,:),B(:,jj)); for k = 1:ca C(ii,jj) = C(ii,jj) + A(ii,kk)*B(kk,jj); end end end Note that ca and rb MUST be the same for the matrices in a matrix multiply to conform for multiplication. The preallocation of C allows us to simply accumulate the results directly in place. This final form is what the code would look like, if you saw it written in (very) old school fortran, etc.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 377687-how-to-multiply-matrices-using-a-for-loop
How to multiply matrices using a for loop - MATLAB Answers - MATLAB Central
January 17, 2018 - https://www.mathworks.com/matlabcentral/answers/377687-how-to-multiply-matrices-using-a-for-loop ยท Cancel Copy to Clipboard ยท I have the equation p(t+1)=p(t)*P. p(t) is the matrix [p1(t) p2(t) p3(t) p4(t)] with p(0)=[1 0 0 0] and P is a 4x4 matrix. I need to create a for loop that runs through the matrix multiplications until it reaches a certain p1(t).
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 129157-creating-a-loop-for-matrix-multiplication
Creating a loop for matrix multiplication - MATLAB Answers - MATLAB Central
May 11, 2014 - Hi. I am trying to create a loop to multiply two matricies 40 times such that A*B0=B1 and A*B1=B2 and so on. So far I have got this % a=1-0.0277; b=(1/15)*(1-0.008); c=0.0215*a; d...
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 1459459-two-3-3-matrix-multiplication-using-for-loop-in-matlab
two 3*3 matrix multiplication using for loop in matlab. - MATLAB Answers - MATLAB Central
September 23, 2021 - FOR or WHILE loop are not advised to use for matrix multiplication. ... https://www.mathworks.com/matlabcentral/answers/1459459-two-3-3-matrix-multiplication-using-for-loop-in-matlab#comment_1755114
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 571342-matrix-multiplication-using-a-for-loop
Matrix multiplication using a for loop - MATLAB Answers - MATLAB Central
July 27, 2020 - I want to make matrix A using elements of matrices P1 and P2 in the following form. Is there any way to do this using a for loop. Any help would be appreciated! A(1,:) = [P1(1,1).*P2(1,1:4) P1(1...
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 432419-matrix-multiplication-using-for-loop
Matrix Multiplication using for loop - MATLAB Answers - MATLAB Central
November 28, 2018 - Suppose I have following 7 matrcies defined as Matrix A x1=1st Matrix x2=2nd Matrix x3=3rd Matrix x4=4th Matrix x5=5th Matrix x6=6th Matrix So I want to multiply 1st element of matr...
Find elsewhere
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 709598-how-to-multiply-matrices-using-for-loop
How to multiply matrices using for loop? - MATLAB Answers - MATLAB Central
January 5, 2021 - How to multiply matrices using for loop?. Learn more about matrices, matrix array, matrix manipulation, matlab
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 1912765-multiply-individual-cells-of-a-matrix-by-a-scalar-using-a-for-loop
Multiply Individual Cells of a Matrix by a Scalar Using a For Loop - MATLAB Answers - MATLAB Central
February 15, 2023 - I have a simple conversion problem where I have a matrix of pressure data (say p1) in hectopascals and need to convert the data to pascals by multiplying the individual elements by the scalar 100. ...
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 273861-matrix-multiplication-after-a-for-loop
matrix multiplication after a for loop - MATLAB Answers - MATLAB Central
March 16, 2016 - You should really understand that code in general (not just Matlab) works differently than mathematical formulas. a(i+1)=a0+i; inside a loop will actually grow a vector which you didn't even create before. I think Matlab should be able to handle it, but it's extremely poor practice and actually won't even run in most languages. Sign in to comment. ... https://www.mathworks.com/matlabcentral/answers/273861-matrix-multiplication-after-a-for-loop#answer_213969
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 1569088-matrix-product-using-for-while-loop
Matrix product using for/while loop - MATLAB Answers - MATLAB Central
October 21, 2021 - I got this task and I should find the error in this function hat should give us the product of 2 martices . function [M_erg] = matrix_mult(M1,M2) M_erg = zeros(size(M1,1),size(M2,2)); for i = 1:...
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 105955-explicit-multiplication-of-matrices-using-for-loops
Explicit multiplication of matrices using for loops - MATLAB Answers - MATLAB Central
November 13, 2013 - Hi, I'm just getting to grips with for loops in Matlab, and I encountered a problem in understanding this specific code: x = [4; 0.5 ; 1]; A = [6 8 1 ; 3 10 1 ; 1 0.5 12.5]; B = [2 0...
๐ŸŒ
MathWorks
uk.mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 667113-how-can-i-multiply-matrices-using-a-for-loop-then-extract-their-elements-after-the-loop
How can I multiply matrices using a for loop then extract their elements after the loop? - MATLAB Answers - MATLAB Central
November 28, 2020 - Have a look at the following code, which uses the initial 'f' value to get the initial matrix, extracts its elements, updates the 'f' value in each iteration to get a new matrix and multiplies the new matrix with the previous one and so on.