How do I make a matrix with a repeating pattern but with different values
Show older comments
I have two variables xi and yi both with size nx1 and I want to create the following matrix:
M= [x1 0 y1; y1 0 x1; x2 0 y2; y2 0 x2; ... ;yn 0 xn].
I've tried using repmat but that just repeats the first row over and over.
Answers (1)
David Hill
on 18 Oct 2022
Edited: David Hill
on 18 Oct 2022
x=randi(100,5,1)
y=randi(100,5,1)
z=zeros(size(x));
M=[x';z';y';y';z';x']
M=reshape(M(:),3,[])'
Categories
Find more on Creating and Concatenating 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!