How to merge two column cell arrays into one, with alternating values?

조회 수: 17 (최근 30일)
Jerki Jokne
Jerki Jokne 2021년 8월 11일
답변: tiwwexx 2021년 8월 11일
A = ones(5,1);
B = zeros(5,1);
Assuming A and B will always be the same size, how can I get the result? I can't seem to find the right way of using reshape to achieve this.
C = {1 0 1 0 1 0 1 0 1 0}
Thanks in advance
  댓글 수: 4
Stephen23
Stephen23 2021년 8월 11일
No need for the second transpose, just specify the desired output size when reshaping:
C = reshape([A,B].',1,[])
Awais Saeed
Awais Saeed 2021년 8월 11일
You are welcome @Jerki Jokne. It’s good that you got the result.

댓글을 달려면 로그인하십시오.

답변 (1개)

tiwwexx
tiwwexx 2021년 8월 11일
I would create a matrix with size a+b then fill it in using steps of two.
a=zeros(1,5);
b=ones(1,5);
c= zeros(1,size(a,2)+size(b,2));
c(1:2:end)=a;
c(2:2:end)=b;

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by