From: Jim on
Good day everyone,

I am a new user to MATLAB and would like some help. I am trying to multiply a 2 matrices as follows:

Firstly I have 2 matrices A and B:


A= [ 91 22 77 31 56 76 9 42 34; 4 5 6 99 54 32 34 54 22; 7 8 9 22 44 55 45 32 87 ]

B = [ 3 6 9 ; 5 4 8; 2 3 3]


I want to multiply them such that I skip every 2 columns in A , that is, i would like to multiply and get the result of:


[ 91 31 9; 4 99 34; 7 22 45] * [3 ; 6; 9]
[ 22 56 42; 5 54 54; 8 44 32] * [ 5; 4 ; 8]
[ 77 76 34; 6 32 22; 9 55 87]* [ 2 ; 3; 3]


Thanks guys
jimmy
From: Matt Fig on
A(:,1:3:end)*B.'
From: Jim on
Matt, thanks for the help but I seem not to get the result I want with your code.

If you multiply the following (as seen in my first post):

[ 91 31 9; 4 99 34; 7 22 45] * [3 ; 6; 9]
[ 22 56 42; 5 54 54; 8 44 32] * [ 5; 4 ; 8]
[ 77 76 34; 6 32 22; 9 55 87]* [ 2 ; 3; 3]


The results should be:

ans =
540
912
558

ans =
670
673
472

ans =
484
174
444

But instead I got the following with your stuff:
ans =

540 651 302
912 688 407
558 483 215

How can i fix this?

thanks
jimmy
From: Matt Fig on
Oops, I read too fast! Your vectors seem to be given by:


A(:,1:3:end)*B(1,:).'
A(:,2:3:end)*B(2,:).'
A(:,3:3:end)*B(3,:).'
From: Jim on
Matt,thanks again. I would like to however make it more efficient without entering many lines.

So I attempted a for loop below:


%=================================================
clear all;clc;

A= [ 91 22 77 31 56 76 9 42 34; 4 5 6 99 54 32 34 54 22; 7 8 9 22 44 55 45 32 87 ]

B = [ 3 6 9 ; 5 4 8; 2 3 3]


for index = 1: 3
result(index) = A(:,index:3:end)
end

result*B

%================================================

I think it should work but I get an error with the "result(index)" int the code.

I would be grateful for some help once again.

Jimmy