Unclear array operation in matlab

3 ビュー (過去 30 日間)
Himanshu
Himanshu 2022 年 9 月 1 日
編集済み: Himanshu 2022 年 9 月 1 日
Dear all, I have created a 5 * 3 array, named as 'conn' and another array as lm which is 1*2 as underneath:
conn= [1 5 6; 2 5 7; 3 4 5 ; 4 5 7; 5 6 8];
lm = [4 ,3];
conn(lm)
while running the code, it gives me the following output.
ans =
4 3
this operation with "conn(lm)" is totally unclear to me when I operate on 5*3 array. I will be grateful if anyone please help me to understand this code.

回答 (2 件)

Chunru
Chunru 2022 年 9 月 1 日
conn= [1 5 6; 2 5 7; 3 4 5 ; 4 5 7; 5 6 8]
conn = 5×3
1 5 6 2 5 7 3 4 5 4 5 7 5 6 8
lm = [4 ,3];
% The following use lm as "linear" index to reference to conn
% The "linear" index count column by column
% so the 4th element of conn is (4,1)
conn(lm)
ans = 1×2
4 3
conn(10) % (5,2)th element of conn
ans = 6
  1 件のコメント
Himanshu
Himanshu 2022 年 9 月 1 日
編集済み: Himanshu 2022 年 9 月 1 日
Thank you very much. This was helpful and precise.

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


weikang zhao
weikang zhao 2022 年 9 月 1 日
Hello, this is matlab's unique indexing mechanism. For a 5*3 matrix, you can use 1-15 to index all elements.
Matlab uses column-major order, so your code is the same as the result below.
coon=[1 2 3 4 5 5 5 4 5 6 6 7 5 7 8];
lm=[4 3];
The output results are organized as follows
conn(lm)=[coon(4) coon(3)]=[4 3]
Incorrect use of '=' operator. Assign a value to a variable using '=' and compare values for equality using '=='.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON
  1 件のコメント
Himanshu
Himanshu 2022 年 9 月 1 日
Thank you very much

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

カテゴリ

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