Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
efficient nested foor loop
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
a1=5:-1:-5;
b1=1:-1:-1;
for i=1:size(b1,2);
for j=1:size(a1,2);
Points(j,:,i) =[a1(1,j) b1(1,i)];% 2D points
end
end
F = reshape( permute(Points, [1 3 2]), [], 2);% 33 2D points
origin=[4.5 3.5];
Total_points=size(a1,2)*size(b1,2);
for i=1:Total_points;
distance(1,i)=sqrt(sum((F(i,:)-Total_points).^2));
original_current=100;
current_survived_first(1,i)=original_current/distance(1,i);
current_survived_second(1,i)=current_survived_first(1,i).*exp(-1.33)
Final_current(1,i)=original_current.*current_survived_second(1,i);
end
This set of matlab codes give me Final_current(1*33 double). Though I also want the same end result, I got it in expense of large intermediate matrices because I am considering all 33 points of interest in all steps. I want to write down nested for loop which first does calculation for 1st point and stores the end result in matrix 'Final_current', then it repeats all steps for remaining points and update the matrix 'Final_current'. So, after I define a1 and b1 I want to write down codes in a different way so that I will be able to store all my results in 'Final_current' matrix but none of intermediate matrices store results for all 33 points.
1 Commento
Image Analyst
il 31 Gen 2012
There's no universe where 33 is considered "large." Or even a thousand times that.
Risposte (1)
Andrei Bobrov
il 31 Gen 2012
a1 = 5:-1:-5;
b1 = 1:-1:-1;
[a b] = ndgrid(a1,b1);
F = [a(:) b(:)];
distance = sqrt(sum((F - size(F,1)).^2,2));
current_survived_first = 100./distance;
current_survived_second = current_survived_first*exp(-1.33);
Final_current = 100*current_survived_second;
OR
Final_current = 1e4./sqrt(sum(([repmat(a1.',numel(b1),1) reshape(repmat(b1,numel(a1),1),[],1)] - numel(a1)*numel(b1)).^2,2))*exp(-1.33);
0 Commenti
Questa domanda è chiusa.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!