Creating a matrix using for loop.

3 ビュー (過去 30 日間)
Janet
Janet 2012 年 7 月 6 日
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

回答 (2 件)

C.J. Harris
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

Andrei Bobrov
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);

カテゴリ

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