Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
loop with manipulation of matrix multiplication

Subject: loop with manipulation of matrix multiplication

From: doug kopet

Date: 12 Aug, 2010 00:04:05

Message: 1 of 8

Hi everyone,

I'm a new matlab user and new to programming and would like to request some help.

I have 2 matrices MM and BB:

        MM = [ 1 2 2 ;
                       5 3 6 ;
                       9 8 7;
                       8 8 4 ;
                       6 1 1 ;
                       5 0 5 ]
        
        BB = [ 31 32 44 ; 20 17 33; 19 44 22]
        
        
        
I would like to multiply in the following manner using a looping procedure :
        
   
 result_part1=[1 2 2; 8 8 4] * [ 31 32 44]'
   
 result_part2 = [ 5 3 6 ; 6 1 1] * [ 20 17 33]'
   
  result_part3 = [9 8 7; 5 0 5]* [19 44 22]'


        
 Then, I would like to output them in the following manner:
        
final_answer = [ 183 349 677; 680 170 205]

cheers
doug

Subject: loop with manipulation of matrix multiplication

From: ImageAnalyst

Date: 12 Aug, 2010 01:56:51

Message: 2 of 8

doug:
Great, it looks like you did it. And you didn't even need to use
looping (which we usually try to avoid).
Now, what is your question?
Does it need to be generalized to some variable number of rows and/or
columns, or what?

Subject: loop with manipulation of matrix multiplication

From: doug kopet

Date: 12 Aug, 2010 02:08:04

Message: 3 of 8



My question is how can I do this procedure, i.e , skip every 2 rows in matrix MM for each of the first 3 rows sequentially and multiply each by its corresponding transposed row in BB.

doug

Subject: loop with manipulation of matrix multiplication

From: doug kopet

Date: 12 Aug, 2010 02:18:06

Message: 4 of 8

Also,my actual work has large matrices that is why I implied that I would I have to use loops.

thanks
doug

Subject: loop with manipulation of matrix multiplication

From: ImageAnalyst

Date: 12 Aug, 2010 02:37:39

Message: 5 of 8

On Aug 11, 10:18 pm, "doug kopet" <onedo...@gmail.com> wrote:
> Also,my actual work has large matrices that is why I implied that I would I have to use loops.
>
> thanks
> doug
-----------------------------------------------------------------------
Well you can start with this:
for startingRow = 1:3
    MM(startingRow:3:end, :) % Display just for info.
    BB(startingRow, :)' % Display just for info.
    % Do the multiplication
    result_part = MM(startingRow:3:end, :) * BB(startingRow)'
end

But I'm not sure how you're getting "final_answer." What's the
algorithm for that? It's not simply summing the result_part's.

Subject: loop with manipulation of matrix multiplication

From: doug kopet

Date: 12 Aug, 2010 03:53:23

Message: 6 of 8

thanks my friend,

i got the answer in your code by jus making small change to the code u suggested.

--> result_part = MM(startingRow:3:end, :) * BB(startingRow,:)'

Can I ask you how to get all the result_part's to store into a single matrix after the looping has finished.

doug

Subject: loop with manipulation of matrix multiplication

From: doug kopet

Date: 12 Aug, 2010 04:24:04

Message: 7 of 8

dont bother,i got it with the indexing

Subject: loop with manipulation of matrix multiplication

From: Roger Stafford

Date: 12 Aug, 2010 04:24:04

Message: 8 of 8

"doug kopet" <onedougk@gmail.com> wrote in message <i3vdpl$dn0$1@fred.mathworks.com>...
> Hi everyone,
>
> I'm a new matlab user and new to programming and would like to request some help.
>
> I have 2 matrices MM and BB:
>
> MM = [ 1 2 2 ;
> 5 3 6 ;
> 9 8 7;
> 8 8 4 ;
> 6 1 1 ;
> 5 0 5 ]
>
> BB = [ 31 32 44 ; 20 17 33; 19 44 22]
>
>
>
> I would like to multiply in the following manner using a looping procedure :
>
>
> result_part1=[1 2 2; 8 8 4] * [ 31 32 44]'
>
> result_part2 = [ 5 3 6 ; 6 1 1] * [ 20 17 33]'
>
> result_part3 = [9 8 7; 5 0 5]* [19 44 22]'
>
>
>
> Then, I would like to output them in the following manner:
>
> final_answer = [ 183 349 677; 680 170 205]
>
> cheers
> doug
- - - - - - - - - - - -
  Try this:

 p = size(MM,1)/size(BB,1);
 final_answer = reshape(sum(MM.*repmat(BB,p,1),2),[],p).';

The two requirements for this generalization are that size(MM,2)=size(BB,2) and size(MM,1) be an integral multiple of size(BB,1).

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us