Clear Filters
Clear Filters

multiplying a 1xM vector by a KxN matrix

4 views (last 30 days)
Hello there,
I have a 1xM vector, say V=[1 -1]; I would like to multiply it by a KxN matrix, say S=[0 0 1 1;1 1 0 0;1 0 1 0;0 1 0 1] such that at the beginning the first element of V gets multiplied by the first columns of S, then the second element of V be multiplied by the first columns of S, then the first element of V gets multiplied by the second column of of S, followed by the multiplication of the second element of V by the second column of S, and so forth. That is for the example mentioned above I get: [0 0 0 0 1 -1 1 -1;1 -1 1 -1 0 0 0 0;1 -1 0 0 1 -1 0 0;0 0 1 -1 0 01 -1]. I would appreciate if somebody can help me please. Thank you!

Accepted Answer

Stephen23
Stephen23 on 9 Nov 2015
Edited: Stephen23 on 9 Nov 2015
Use bsxfun, reshape, and permute:
>> V = [1,-1]
V =
1 -1
>> S = [0,0,1,1; 1,1,0,0; 1,0,1,0; 0,1,0,1]
S =
0 0 1 1
1 1 0 0
1 0 1 0
0 1 0 1
>> X = bsxfun(@times,S,reshape(V,1,1,[]));
>> reshape(permute(X,[3,2,1]),[],size(S,1)).'
ans =
0 0 0 0 1 -1 1 -1
1 -1 1 -1 0 0 0 0
1 -1 0 0 1 -1 0 0
0 0 1 -1 0 0 1 -1

More Answers (0)

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!