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!