Need help in uderstanding this piece of code

1 回表示 (過去 30 日間)
adi kul
adi kul 2016 年 7 月 7 日
編集済み: Thorsten 2016 年 7 月 7 日
Hello All, This is the code I found somewhere in a script :
output(idx,:) = [y1(ii),z,smax];
Can anyone explain me the logic here? I mean what's expected out of this code?
  1 件のコメント
Stephen23
Stephen23 2016 年 7 月 7 日
編集済み: Stephen23 2016 年 7 月 7 日
These are all very basic MATLAB operations: indexing, concatenation, and assigning to variables. You should do the introductory tutorials, which introduce these basic MATLAB operations:

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

採用された回答

Adam
Adam 2016 年 7 月 7 日
idx defines the row of the 'output' matrix being written to and ':' says to just fill as many columns in that row as there are outputs from the right-hand side of the equation.
The stuff on the right is just an array of variables, the first being indexed into by some other variable so I assume in this case there are 3 elements in that vector and these will fill the 3 columns for row idx in 'output'.
  1 件のコメント
Walter Roberson
Walter Roberson 2016 年 7 月 7 日
In the code it is not strictly necessary that z or smax be scalars; they could be vectors, as long as the total length of the three variables matched the number of columns in the output array.

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

その他の回答 (1 件)

Thorsten
Thorsten 2016 年 7 月 7 日
編集済み: Thorsten 2016 年 7 月 7 日
You generate a row vector
[y1(ii),z,smax]
And this vector is assigned to row "idx" of the output matrix
output(idx,:) = [y1(ii),z,smax];
You cannot do
output(idx) = [y1(ii),z,smax];
because you cannot assign a 1xN vector to a single number output(idx). So you need the ,: to assign the vector to a row.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by