Discussion:
[newbie] problem with for loop
(too old to reply)
nukeymusic
2009-06-08 13:20:06 UTC
Permalink
I'm trying to fill an array with the following script:
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end

this doesn't work however, the strange thing is when I don't use a for
loop but just enter lines one and two incrementing n myself each time
it I get the expected result

any help welcome (please don't shoot at a newbie)

nukeymusic
Mario Baussmann
2009-06-08 13:37:05 UTC
Permalink
try: endfor instead of end ;-)
Mario
Post by nukeymusic
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end
this doesn't work however, the strange thing is when I don't use a for
loop but just enter lines one and two incrementing n myself each time
it I get the expected result
any help welcome (please don't shoot at a newbie)
nukeymusic
nukeymusic
2009-06-08 13:57:38 UTC
Permalink
Post by Mario Baussmann
try: endfor instead of end ;-)
Mario
Post by nukeymusic
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end
this doesn't work however, the strange thing is when I don't use a for
loop but just enter lines one and two incrementing n myself each time
it I get the expected result
any help welcome (please don't shoot at a newbie)
nukeymusic
I tried that but the result is not OK as you can see from this (I put
the three lines in a file calc_series.m):
octave:1> calc_series
octave:2> x(1)
error: invalid matrix index = 1

I expected an array 1 2 2 3 3 3 4 4 4 4

I'm surely doing something wrong but what?


thanks for any help offered

nukeymusic
Mirek
2009-06-08 14:54:47 UTC
Permalink
Post by Mario Baussmann
try: endfor instead of end ;-)
Mario
Post by nukeymusic
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end
n.*(n-1)./2+1 is greather then n.*(n-1)./2 then the result is
an empty range.
for n=1:5;disp(n.*(n-1)./2:n.*(n-1)./2+1);end
0 1
1 2
3 4
6 7
10 11
octave:2> x(1)
error: invalid matrix index = 1
because array x is empty :)
I expected an array 1 2 2 3 3 3 4 4 4 4
My solusion is:

octave:54> n=4;x=zeros(1,n*(n+1)/2),x(cumsum(0:n-1)+1)=1,x=cumsum(x)
x =

0 0 0 0 0 0 0 0 0 0

x =

1 1 0 1 0 0 1 0 0 0

x =

1 2 2 3 3 3 4 4 4 4
nukeymusic
2009-06-08 15:22:02 UTC
Permalink
On Jun 8, 4:54 pm, Mirek
Post by Mirek
Post by nukeymusic
Post by Mario Baussmann
try: endfor instead of end ;-)
Mario
Post by nukeymusic
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end
n.*(n-1)./2+1 is greather then n.*(n-1)./2 then the result is
an empty range.
Oh dear, how stupid from me, it should have been:
x(n.*(n-1)./2+1:n.*(n+1)./2)=n;
after running the script like this x(1:10) contains the right numbers
The solution you offer below is probably better (less cpu-cycles?) for
large arrays
thanks a again for the help offered

nukeymusic
Post by Mirek
Reversing limits doesn't solve your problem:> for n=1:5;disp(n.*(n-1)./2:n.*(n-1)./2+1);end
0 1
1 2
3 4
6 7
10 11
Post by nukeymusic
octave:2> x(1)
error: invalid matrix index = 1
because array x is empty :)
Post by nukeymusic
I expected an array 1 2 2 3 3 3 4 4 4 4
octave:54> n=4;x=zeros(1,n*(n+1)/2),x(cumsum(0:n-1)+1)=1,x=cumsum(x)
x =
0 0 0 0 0 0 0 0 0 0
x =
1 1 0 1 0 0 1 0 0 0
x =
1 2 2 3 3 3 4 4 4 4
nukeymusic
2009-06-08 15:31:41 UTC
Permalink
On Jun 8, 4:54 pm, Mirek
Post by Mirek
Post by nukeymusic
Post by Mario Baussmann
try: endfor instead of end ;-)
Mario
Post by nukeymusic
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end
n.*(n-1)./2+1 is greather then n.*(n-1)./2 then the result is
an empty range.
Reversing limits doesn't solve your problem:> for n=1:5;disp(n.*(n-1)./2:n.*(n-1)./2+1);end
0 1
1 2
3 4
6 7
10 11
Post by nukeymusic
octave:2> x(1)
error: invalid matrix index = 1
because array x is empty :)
Post by nukeymusic
I expected an array 1 2 2 3 3 3 4 4 4 4
octave:54> n=4;x=zeros(1,n*(n+1)/2),x(cumsum(0:n-1)+1)=1,x=cumsum(x)
x =
0 0 0 0 0 0 0 0 0 0
x =
1 1 0 1 0 0 1 0 0 0
x =
1 2 2 3 3 3 4 4 4 4
could you also tell how to make a function out of this (with n as a
parameter)?

thanks in advance
nukeymusic
nukeymusic
2009-06-08 19:12:25 UTC
Permalink
Post by nukeymusic
On Jun 8, 4:54 pm, Mirek
Post by Mirek
Post by nukeymusic
Post by Mario Baussmann
try: endfor instead of end ;-)
Mario
Post by nukeymusic
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end
n.*(n-1)./2+1 is greather then n.*(n-1)./2 then the result is
an empty range.
Reversing limits doesn't solve your problem:> for n=1:5;disp(n.*(n-1)./2:n.*(n-1)./2+1);end
0 1
1 2
3 4
6 7
10 11
Post by nukeymusic
octave:2> x(1)
error: invalid matrix index = 1
because array x is empty :)
Post by nukeymusic
I expected an array 1 2 2 3 3 3 4 4 4 4
octave:54> n=4;x=zeros(1,n*(n+1)/2),x(cumsum(0:n-1)+1)=1,x=cumsum(x)
x =
0 0 0 0 0 0 0 0 0 0
x =
1 1 0 1 0 0 1 0 0 0
x =
1 2 2 3 3 3 4 4 4 4
could you also tell how to make a function out of this (with n as a
parameter)?
thanks in advance
nukeymusic
I figured it out myself -though I'm not sure whether this is the best
practice-:

function answer = calc_sequence(k)
%calc_sequence
for n=1:k
x(n.*(n-1)./2+1:n.*(n+1)./2)=n;
endfor
answer = x(1:k);
endfunction

Mario Baussmann
2009-06-08 13:46:14 UTC
Permalink
.. and maybe you mean x(..,..) to adress the matrix
instead of x(..:..)?
Mario
Post by nukeymusic
clear
for n=1:10
x(n.*(n-1)./2+1:n.*(n-1)./2)=n;
end
this doesn't work however, the strange thing is when I don't use a for
loop but just enter lines one and two incrementing n myself each time
it I get the expected result
any help welcome (please don't shoot at a newbie)
nukeymusic
Continue reading on narkive:
Loading...