Generating linear array [0 -1 0 1 -2 -1 0 1 2 ......]
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2020-5-13
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 个评论
Rik
2020-5-13
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)
更多回答(2 个)
Rik
2020-5-13
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 个评论
Rik
2020-5-13
编辑:Rik
2020-5-13
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
Mehmed Saad
2020-5-13
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 个评论
另请参阅
类别
在 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!