array assignment with and without semicolon

5 次查看(过去 30 天)
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)

采纳的回答

Thorsten
Thorsten 2015-12-1
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by