array assignment with and without semicolon

1 view (last 30 days)
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)

Accepted Answer

Thorsten
Thorsten on 1 Dec 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 Comments
Philip
Philip on 1 Dec 2015
Thanks for the explanation; now (1) runs because there is no such restriction ?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!