how to get following matrix using bsxfun
Show older comments
I have a matrix x=[0 0 0;5 8 0; 7 6 0].
I want a matrix m=[0 0 0; 5 8 0 ; 7 6 0 ; 0 0 8; 5 8 8; 7 6 8; 0 0 16; 5 8 16; 7 6 16] I want that the third column of matrix x gets multiplied each time by 8 while other two columns remain same. I want this to continue until the value in third column reaches 72. how can i do it with bsxfun??
Accepted Answer
More Answers (1)
Andrei Bobrov
on 7 Feb 2015
Edited: Andrei Bobrov
on 7 Feb 2015
k = 0:72/8;
M = repmat(x,numel(k),1);
add = 8*ones(size(x,1),1)*k;
M(:,3) = add(:);
with bsxfun
m = reshape(bsxfun(@plus,permute(x,[1 3 2]),...
bsxfun(@times,reshape([0 0 1],1,1,[]),0:72/8)),[],3);
1 Comment
Ace_ventura
on 8 Feb 2015
Categories
Find more on Matrices and Arrays 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!