MATLAB Answers

Generating linear array [0 -1 0 1 -2 -1 0 1 2 ......]

2 views (last 30 days)
Hi
So, I'd like to generate a linear array with output like this
[0 -1 0 1 -2 -1 0 1 2 -3 -2 -1 0 1 2 3 -4 -3 -2 -1 0 1 2 3 4 and so on ]
using for loop. I'm not sure whether to use one or two (nested) for loop
the code should be on the following
n indicates the order and m goes from -n to n with n increases. Another thing, is also the negative indices, this I think can be solved by using a temporary value like temp = 1:length(m). But overall I'm not so sure how should I put everything into the code
for i = 0:n
m = -n:n
.....
end

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 13 May 2020
N = 4;
C = cell(1,N);
for k = 1:N
C{k} = -k:k;
end
V = [0,C{:}]
Giving:
V =
0 -1 0 1 -2 -1 0 1 2 -3 -2 -1 0 1 2 3 -4 -3 -2 -1 0 1 2 3 4

  2 Comments

Rik
Rik on 13 May 2020
It seems I was overthinking it again. This is faster than my code, except for N=4 (and sometimes 1 and 5):
n_list=round(10.^(0:0.1:4));
runtime1=zeros(1,numel(n_list));
runtime2=zeros(1,numel(n_list));
for n_ind=1:numel(n_list)
n=n_list(n_ind);
tic
output_1=cell2mat(arrayfun(@(x) -x:x,0:n,'UniformOutput',false));
runtime1(n_ind)=toc;
tic
C = cell(1,n+1);
for k = 0:n
C{k+1} = -k:k;
end
V = [C{:}];
runtime2(n_ind)=toc;
end
figure(1),clf(1)
plot(n_list,runtime1,n_list,runtime2)
xlabel('n'),ylabel('time to execute')
legend('array','loop','Location','NorthWest')
clc
n_list(runtime1<runtime2)
FlyingSpoon
FlyingSpoon on 14 May 2020
thank you, for your input both of you. Stephen answer is more likely closer to what I really want. The reason is I'd like to get the idea how to make this column look like this, because I want to do some calculations based on this value. I'm still imagining it how to proceed with this
thank you

Sign in to comment.

More Answers (2)

Rik
Rik on 13 May 2020
No need for a for-loop:
n=4;
output=cell2mat(arrayfun(@(x) -x:x,0:n,'UniformOutput',false));
%test against your example
example=[0 -1 0 1 -2 -1 0 1 2 -3 -2 -1 0 1 2 3 -4 -3 -2 -1 0 1 2 3 4];
isequal(example,output)

  2 Comments

FlyingSpoon
FlyingSpoon on 13 May 2020
thank you, but the reason it should be for, because i'm trying to understand how to use for loop, i know it's probably not as fast compared to this
Rik
Rik on 13 May 2020
I'm not going to give you working code with a for-loop because that starts to sound like solving homework.
If you insist on a for-loop: try to answer the question what is different between n=3 and n=4:
m=-4:4;
output_for_n_is_4=[output_for_n_is_3 m];
So if you find the number of element that a given n will result in you can use this to find the indices:
ind=(numel_for_n_is_3+1):numel_for_n_is_4;
output(ind)=m;
At this point the question is how to figure out a way to find the value of numel_for_n_is_4. Luckily you already have working code:
clc
for n=1:5
fprintf('for n=%d length is %d\n',n,numel(cell2mat(arrayfun(@(x) -x:x,0:n,'UniformOutput',false))))
end
%for n=1 length is 4
%for n=2 length is 9
%for n=3 length is 16
%for n=4 length is 25
%for n=5 length is 36
Do you happen to recognize this sequence of numbers?
The speed improvement is not tremendous by the way, only about 40% for small n and 20% for large n:
clc,clear
n_list=round(10.^(0:0.1:4));
runtime1=zeros(1,numel(n_list));
runtime2=zeros(1,numel(n_list));
for n_ind=1:numel(n_list)
n=n_list(n_ind);
tic
output_1=cell2mat(arrayfun(@(x) -x:x,0:n,'UniformOutput',false));
runtime1(n_ind)=toc;
tic
output_2=zeros(1,____);
for k=____
ind=____;
output_2(ind)=____;
end
runtime2(n_ind)=toc;
end
figure(1),clf(1)
plot(n_list,runtime1,n_list,runtime2)
xlabel('n'),ylabel('time to execute')
legend('array','loop','Location','NorthWest')
runtime1.\runtime2

Sign in to comment.


Mehmed Saad
Mehmed Saad on 13 May 2020
ii = 0:4;
jj = -1:-1:-5;
pos_cell = arrayfun(@(x) 0:x,ii,'uni',0);
neg_cell = arrayfun(@(x) -1:-1:x,jj,'uni',0);
all = [pos_cell;neg_cell];
all = [all{:}]

  0 Comments

Sign in to comment.

Tags

Products


Translated by