How do I make a matrix with a repeating pattern but with different values

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)

x=randi(100,5,1)
x = 5×1
61 44 71 31 36
y=randi(100,5,1)
y = 5×1
92 71 73 33 100
z=zeros(size(x));
M=[x';z';y';y';z';x']
M = 6×5
61 44 71 31 36 0 0 0 0 0 92 71 73 33 100 92 71 73 33 100 0 0 0 0 0 61 44 71 31 36
M=reshape(M(:),3,[])'
M = 10×3
61 0 92 92 0 61 44 0 71 71 0 44 71 0 73 73 0 71 31 0 33 33 0 31 36 0 100 100 0 36

Categories

Tags

Asked:

MN
on 18 Oct 2022

Edited:

on 18 Oct 2022

Community Treasure Hunt

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

Start Hunting!