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.

답변 (1개)

David Hill
David Hill 2022년 10월 18일
편집: David Hill 2022년 10월 18일
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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

MN
2022년 10월 18일

편집:

2022년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by