Understanding eye / identity matrix in matlab

5 ビュー (過去 30 日間)
Elhoussine Talab
Elhoussine Talab 2016 年 9 月 5 日
コメント済み: umair Majeed 2021 年 11 月 22 日
Hello,
I am working with a machine learning alogrithm and these two lines of code have helped me but I still do not understand what they do exactly, specially the last one
eye_matrix = eye(10);
y_matrix = eye_matrix(y, :);
so 'y' is a vector consisting of numbers of 1 to 10 i.e: y = [1;2;3;4;5;6;7;8;9;10]
The result y matrix is a matrix with 10 rows and 10 columns, every row has zeros everywhere, but one in an index of the corresponding row value in y.
can someone how did we achieve this, and how the second line of code works.
  1 件のコメント
umair Majeed
umair Majeed 2021 年 11 月 22 日
how can I extract a row of eye matrix without saving it in any variable.
means instaed of using
a= eye (5);
then a( : , 1)
I want to extract rows with out assigning it to another varial somewhat like this eye(5)( : , 1)

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

採用された回答

Pawel Ladosz
Pawel Ladosz 2016 年 9 月 5 日
Hi, eye matrix creates a identity matrix (so all values zeros apart from diagonal terms). Then the second line tells matlab to take that identity matrix and extract rows with indexes contained in y. As y contains all rows (1 to 10) you end up with same identity matrix.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by