array assignment with and without semicolon

1 visualización (últimos 30 días)
Philip
Philip el 1 de Dic. de 2015
Comentada: Thorsten el 1 de Dic. de 2015
If this is already be in the FAQ please direct me to the relevant FAQ section, if not here is my question I am assigning parts of an larger array to a smaller array I am using it in a loop, my statement are (1) and (2); (1) runs without any error base on my limited understanding of the use of the semicolon (2) should run as well but Matlab returns an error "Subscripted assignment dimension mismatch".
a and b changes inside the loop, can someone help understand what the issue is with the use of (2)? Thanks, Philip
xbit_1 = xbit(a:b,1); (1)
xbit_1(:,1) = xbit(a:b,1); (2)

Respuesta aceptada

Thorsten
Thorsten el 1 de Dic. de 2015
The problem is when a and b change such that the number of elements in a:b change. If you have, say, n elements in a:b for the first run of the loop, xbit_1 is n x 1. If now the number of elements in a:b change in another run of the loop, say to n + 3, you try to assign n+3 values to a n x 1 matrix. That does not work. Instead, use
xbit_1(1:numel(a:b),1) = xbit(a:b,1);
  2 comentarios
Philip
Philip el 1 de Dic. de 2015
Thanks for the explanation; now (1) runs because there is no such restriction ?
Thorsten
Thorsten el 1 de Dic. de 2015
Exactly.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by