Effacer les filtres
Effacer les filtres

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

26 vues (au cours des 30 derniers jours)
Jerki Jokne
Jerki Jokne le 11 Août 2021
Réponse apportée : tiwwexx le 11 Août 2021
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 commentaires
Stephen23
Stephen23 le 11 Août 2021
No need for the second transpose, just specify the desired output size when reshaping:
C = reshape([A,B].',1,[])
Awais Saeed
Awais Saeed le 11 Août 2021
You are welcome @Jerki Jokne. It’s good that you got the result.

Connectez-vous pour commenter.

Réponses (1)

tiwwexx
tiwwexx le 11 Août 2021
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;

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by