-4

I have a matrix:
1|2|3|4
4|5|6|7
7|8|9|10
10|11|12|13
I want to multiply the indices of this matrix with indices of another matrix of different size:
7|8|9
9|10|10
10|11|11
for these two matrices I have used the following for loops:

for x=1:4  
  for y=1:4  
     for m=1:3  
       for n=1:3  
          c=(m*x+n*y);
       end
     end
  end
end

Is there any way to rewrite the above code without using loops? If the indices of each element can be generated in the above matrices, I think it can be done. Please help

Ajax
  • 17
  • 3
  • 1
    You have only 3 columns in the last row of the first matrix while all other rows have 4 columns. This is not possible. And what output are you expecting ? – Sardar Usama Mar 14 '17 at 12:06

1 Answers1

0
mx = m'*x; 
mx = mx(:);
ny = n'*y;
ny = ny(:);
mxe = repmat(mx, [length(ny), 1]);
nye = repmat(ny, [length(mx), 1]);
c = mxe+nye;

This will result in c containing all the values that get put in during that loop you have there (note that in your loop, value gets assigned and overwritten).

Zizy Archer
  • 1,392
  • 7
  • 11
  • I applied the same to two different sized matrices of order 256x256 and 128x128 and i got the following error message: ??? Maximum variable size allowed by the program is exceeded. Error in ==> repmat at 90 mind = mind(:,ones(1,siz(1))); please help – Ajax Mar 15 '17 at 02:54
  • size(mx) 32768 1
    size(ny) 32768 1
    – Ajax Mar 15 '17 at 06:31