Creating Identity Vectors Using FOR Loop

5 ビュー (過去 30 日間)
JENIZA CHALA
JENIZA CHALA 2018 年 3 月 4 日
回答済み: David Fletcher 2018 年 3 月 4 日
I have a homework assignment where I need to solve something much larger than I am asking, but I am having trouble with how I want to start it off. I need to use the vectors of an Identity Matrix to solve for a matrix A.
I want to create these vectors [1; 0; 0; 0], [0; 1; 0; 0], ... as 1 moves down the vector in each one produced. I tried using a FOR loop for this and it did not work. I tried using:
I = zeros(4,1);
for k = 1:4
I(k,1) = 1;
end
Now, when I do this, it creates [1; 0; 0; 0], [1; 1; 0; 0], ... [1; 1; 1; 1] and I end up with an all 1 4x1 vector. I don't know how to make this work, and I cannot move on to the rest of the assignment without this part.
  3 件のコメント
JENIZA CHALA
JENIZA CHALA 2018 年 3 月 4 日
I had originally used that and just separated the matrix, but she wants a for loop creating the separate vectors of the identity matrix.
David Fletcher
David Fletcher 2018 年 3 月 4 日
I = zeros(4,4);
for k = 1:4
I(k,k) = 1;
end

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

採用された回答

David Fletcher
David Fletcher 2018 年 3 月 4 日
Or if you really wanted to make them all totally separate...
A = zeros(1,4);
B = zeros(1,4);
C = zeros(1,4);
D = zeros(1,4);
for k = 1:4
switch k
case 1
A(1,1) = 1
case 2
B(1,2) = 1
case 3
C(1,3) = 1
case 4
D(1,4) = 1
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by