Clear Filters
Clear Filters

How to multiply two matrices

3 views (last 30 days)
Dineth Senevirathne
Dineth Senevirathne on 14 May 2017
Answered: Andrei Bobrov on 14 May 2017
Hello i have two matrices.
  1. a 2749*1 matrix
  2. a 500*1 matrix
i want to multiply above matrices like this
  • first 500 rows of 1st matrix with 2nd matrix and store the value
  • jump 250 rows (now we are in 750th row)
  • then 2nd 500 rows (from 750th row to 1250th row) of 1st matrix with 2nd matrix and store the valuelikewise.you have to get the transpose of the first matrix to match the dimension to multiply. (1*500 and 500*1)
  1 Comment
John D'Errico
John D'Errico on 14 May 2017
So I am forced to wonder why two people are asking virtually the same question. Sounds like homework to me.

Sign in to comment.

Answers (3)

Michael Saniuk
Michael Saniuk on 14 May 2017
>> A = rand(2749,1);
>> B = rand(500,1);
>> C = A(1:500,1).*B;
>> D = A(751:1250,1).*B;

Star Strider
Star Strider on 14 May 2017
What output do you want from your vector multiplication?
Try this:
A = randi(9, 1, 2500); % Create Data
B = randi(9, 500, 1); % Create Data
for k1 = 1:2
Result(:,k1) = A(1, (1:500)+(k1-1)*250).' .* B;
end
This does element-wise multiplication, producing a vector in ‘Result’. If you want a single value (dot-product), change ‘.*’ to ‘*’.

Andrei Bobrov
Andrei Bobrov on 14 May 2017
A - vector 2749 x 1
B - vector 500 x 1
A1 = reshape(A(1:2*749),749,[]);
out = A1(1:500,:).' * B; % dot-product
out = A1(1:500,:) .* B; % element-wise MATLAB >=R2016b
out = bsxfun(@times,A1(1:500,:),B); % element-wise MATLAB >=R2016b

Community Treasure Hunt

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

Start Hunting!