How to sdcsd ?

2 ビュー (過去 30 日間)
H
H 2019 年 10 月 3 日
編集済み: H 2019 年 11 月 27 日
c
  2 件のコメント
Shubham Gupta
Shubham Gupta 2019 年 10 月 3 日
Not entirely sure, but is this what you want ?
V = [0,0,0,0]';
i = 2; % change i to assign 1 to the corresponding index
V(i) = 1; % generated V = [0; 2; 0; 0]
You said you 1 vector but your shows 4 different vectors with different name, what output do you want exactly?
H
H 2019 年 10 月 3 日
Sorry for the confusion. Actually I want to have N number of vectors. And I gave examples of first 4 of those.
v = zeros(N,1);
for i = 1:N
v(i) = 1
end
When I do the above, I get all ones in one vector. But I want to generate N number of vectors, depending on the i value, the value "1" would take place on that specific cell, other cells would remain zero.

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

回答 (2 件)

John D'Errico
John D'Errico 2019 年 10 月 3 日
Don't. Instead, use arrays to store them.
n = 4;
V = eye(n);
V
V =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
As you can see, the rows of V are exactly as you wish. You can access the i'th such vector using indexing.
V(2,:)
ans =
0 1 0 0

Steven Lord
Steven Lord 2019 年 10 月 3 日
Rather than making a large number of variables (which will clutter your workspace and be difficult to work with) instead I recommend making one variable and accessing parts of it as needed.
V = eye(4);
for k = 1:size(V, 2)
x = V(:, k) % Equivalent of Vk
% Work with x
end
Actually, if you want to iterate over the columns of V, for has an interesting property if you specify the range over which to iterate as a matrix.
V = eye(4);
for vec = V
disp(vec)
end
vec will take on each of the columns of V in turn.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by