Generating linear array [0 -1 0 1 -2 -1 0 1 2 ......]
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Stephen23
le 13 Mai 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 commentaires
Rik
le 13 Mai 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)
Plus de réponses (2)
Rik
le 13 Mai 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 commentaires
Rik
le 13 Mai 2020
Modifié(e) : Rik
le 13 Mai 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
Mehmed Saad
le 13 Mai 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 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!