How Does This Short Code Work?

1 ビュー (過去 30 日間)
Rightia Rollmann
Rightia Rollmann 2017 年 3 月 11 日
編集済み: Stephen23 2017 年 3 月 12 日
How is D calculated? Could you explain the steps that MATLAB takes to calculate D?
Is it only for logical values or can be extended to a wider range?
Is there any link to learn more about it?
A = [1; 1; 0; 0; 0; 0; 1];
C = {'w','b'};
D = C(1+A)
D =
'b' 'b' 'w' 'w' 'w' 'w' 'b'
  1 件のコメント
Stephen23 2017 年 3 月 12 日
編集済み: Stephen23 2017 年 3 月 12 日



Star Strider
Star Strider 2017 年 3 月 11 日
The ‘A’ matrix is a double precision numeric array, not a logical array. Your ‘C’ cell has 2 elements, 'w' and 'b' so ‘C(1)='w'’, and ‘C(2)='b'’. MATLAB indexing begins at 1, not 0, so it is necessary to add 1 to ‘A’ to correctly address the elements of ‘C’.
It can be extended, providing that either no element of ‘A’ is greater that 1, or that you add an appropriate number of elements to ‘C’ to allow for elements of ‘A’ that are greater than 1.
An appropriate link to learn more about it is in the documentation on Matrix Indexing.

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by