Creating a matrix using for loop.
5 次查看(过去 30 天)
显示 更早的评论
I am pretty new to matlab and I am trying create a matrix from a column vector.
The problem:
(This is a simplified version of my 100 x 1 vector and 100 x100 matrix.)
The column vector
a= [.12
.1
.2
.3
.4
.5
.6
.7
.8
.9]
b= zeros(10,10)
I am trying to get
0 0 0 0 0 0 0 0 0 0
.12 0 0 0 0 0 0 0 0 0
.1 .12 0 0 0 0 0 0 0 0
.2 .1 .12 0 0 0 0 0 0 0
.3 .2 .1 .12 0 0 0 0 0 0
.4 .3 .2 .1 .12 0 0 0 0 0
.5 .4 .3 .2 .1 .12 0 0 0 0
.6 .5 .4 .3 .2 .1 .12 0 0 0
.7 .6 .5 .4 .3 .2 .1 .12 0 0
.8 .7 .6 .5 .4 .3 .2 .1 .12 0
.9 .8 .7 .6 .5 .4 .3 .2 .1 .12
is there a way to create a loop that will do this?
I have the start of a loop that will put the 1st value in the 1st column and the 2nd in the second column and so on and so forth. But everything I try, to get it to sort or shift does not get me the values in the places I need. I know I am missing something simple but I cant figure it out.
Thanks for the help! This is the start of the loop I have.
for i=1:100;
f(i+1:100,i)=r(i);
%r is the 100 x 1 column all values are greater than zero less than 1
0 个评论
回答(2 个)
C.J. Harris
2012-7-6
编辑:C.J. Harris
2012-7-6
Try this:
a = [0.12;0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.8;0.9]
f = zeros(length(a));
for b=length(a):-1:1
f(:,length(a)+1-b) = [zeros(length(a)-b,1);a(1:b)];
end
0 个评论
Andrei Bobrov
2012-7-6
编辑:Andrei Bobrov
2012-7-6
b = [zeros(1,numel(a));tril(toeplitz(a))];
or
b = toeplitz([0;a],zeros(1,numel(a)));
or
a1 = [0;a];
i1 = (1:numel(a1))';
idx = bsxfun(@minus,i1,0:i1(end)-2);
idx(idx<=0)=1;
b = a1(idx);
0 个评论
另请参阅
类别
在 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!