create vectors associated with each entry of an array and save them in a new matrix
2 次查看(过去 30 天)
显示 更早的评论
Say i have a matrix like A=[1 2; 3 4], and that i need to create 4, vectors each one associated to one entrance of the matrix, such that the first one goes from -1..1, and second from -2..2, and so forth. Wath i try was
for j=1:2
for k=1:2
W=linspace(-A(j,k),A(j,k),4)
end
end
the problem with that line is that it not save the data. Also i need that to create a new matrix, such that every row be one of the vectors that i mentioned.
0 个评论
采纳的回答
更多回答(1 个)
Matt Tearle
2014-2-20
编辑:Matt Tearle
2014-2-20
The easy, brute-force way is just to append the new return from linspace to W each time. Start with W as an empty array:
A=[1 2; 3 4];
W = [];
for j=1:2
for k=1:2
W = [W;linspace(-A(j,k),A(j,k),4)];
end
end
If you're doing this with A large, growing an array like this is not very nice, so instead
A=[1 2; 3 4];
n = size(A,1);
W = zeros(n^2);
for j=1:n
for k=1:n
W((j-1)*n+k,:) = linspace(-A(j,k),A(j,k),n^2);
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!