Creating a matrix using for loop.

3 vues (au cours des 30 derniers jours)
Janet
Janet le 6 Juil 2012
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

Réponses (2)

C.J. Harris
C.J. Harris le 6 Juil 2012
Modifié(e) : C.J. Harris le 6 Juil 2012
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 le 6 Juil 2012
Modifié(e) : Andrei Bobrov le 6 Juil 2012
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);

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by