Defining multiple arrays at once?

I am writing a code which requires a few zero arrays of the same dimension for a summation process in a loop, and I was wondering if it is possible to define them all at once. For example, I want 3 vectors A to C
A=zeros(5,1);
B=zeros(5,1);
C=zeros(5,1);
How can this be replaced with one line (if possible)? Thanks.

 Respuesta aceptada

Guillaume
Guillaume el 19 de Sept. de 2014

5 votos

[A, B, C] = deal(zeros(5,1));

4 comentarios

Xen
Xen el 19 de Sept. de 2014
Works like a charm! Thanks.
Kien Pham
Kien Pham el 3 de Sept. de 2018
Do you know how to do this for cells?
Stephen23
Stephen23 el 3 de Sept. de 2018
@Kien Pham: what do you mean?:
  • one cell array dealt to three cell arrays.
  • three inputs dealt to one cell array.
  • three cell arrays dealt to three cell arrays.
  • three cell arrays dealt to one cell array.
  • something else?
There are lots of ways that deal can be used with cell arrays, but you have not given us any idea of what data you have, and what you are trying to achieve with it.
Kien Pham
Kien Pham el 17 de Sept. de 2018
Editada: Kien Pham el 17 de Sept. de 2018
@Stephen Colbeldick: I was trying to deal one cell to multiple cell arrays. While trying to clarify my question to you, I was able to answer my own question by looking up deal.
Thank you anyway for your response. It really helps me think about how I write my questions to the Community next time.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

Xen
el 19 de Sept. de 2014

Editada:

el 17 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by