Can somebody explain me this answer?

1 回表示 (過去 30 日間)
Marko Kosijer
Marko Kosijer 2019 年 9 月 9 日
回答済み: Marko Kosijer 2019 年 9 月 9 日
a=[1 2; 3 4]; a(3*ones(2)) ans= 2 2 2 2

採用された回答

madhan ravi
madhan ravi 2019 年 9 月 9 日
The code copies the third element of a as 2 by 2 matrix, see the below example:
>> a
a =
1 2
3 4
>> a(:)
ans =
1
3
2
4
>> a([3,3;3,3])
ans =
2 2
2 2
>>

その他の回答 (3 件)

James Tursa
James Tursa 2019 年 9 月 9 日
編集済み: James Tursa 2019 年 9 月 9 日
You are using linear indexing into "a". This matrix:
>> 3*ones(2)
ans =
3 3
3 3
When used as indexing, it means "create a 2x2 matrix result, and put the 3rd element of "a" into that matrix result at each spot". Since the 3rd element of "a" in memory is the value 2, you get a 2x2 matrix of all 2's as your answer.

Bob Thompson
Bob Thompson 2019 年 9 月 9 日
a=[1 2; 3 4]; % Dictates a matrix, 'a,' and it values. Is a 2x2 matrix
a(3*ones(2)) % Calls the elements of 'a' which are located at 3*a 2x2 matrix of ones.
ans= 2 2 2 2 % This answer is returned because a(3) is the element in the first row, second column, a 2, and all four elements of the ones matrix will call the same element of 'a.'

Marko Kosijer
Marko Kosijer 2019 年 9 月 9 日
thank you so much guys!

カテゴリ

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