array assignment with and without semicolon

5 ビュー (過去 30 日間)
Philip
Philip 2015 年 12 月 1 日
コメント済み: Thorsten 2015 年 12 月 1 日
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);
  2 件のコメント
Philip
Philip 2015 年 12 月 1 日
Thanks for the explanation; now (1) runs because there is no such restriction ?
Thorsten
Thorsten 2015 年 12 月 1 日
Exactly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by