フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

efficient nested foor loop

1 回表示 (過去 30 日間)
Bibek
Bibek 2012 年 1 月 30 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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 件のコメント
Image Analyst
Image Analyst 2012 年 1 月 31 日
There's no universe where 33 is considered "large." Or even a thousand times that.

回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2012 年 1 月 31 日
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);

この質問は閉じられています。

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by