フィルターのクリア

Info

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

how to speed up this nestled for loop

3 ビュー (過去 30 日間)
Jeroen
Jeroen 2012 年 2 月 17 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hi everybody,
I need to make this code faster:
for i=1:n1*n2*n3;
x1 = M(i,1);
y1 = M(i,2);
ex1 = M(i,4);
ey1 = M(i,5);
ez1 = M(i,6);
if x1 <= 70 && x1 >= -70 && y1 <= 70 && y1 >= -70;
ex2 (i) = 0;
ey2 (i) = 0;
ez2 (i) = 0;
else
ex2 (i) = ex1;
ey2 (i) = ey1;
ez2 (i) = ez1;
end
end
What is the best way to do that?

回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2012 年 2 月 17 日
try this is code
exyz = zeros(size(M(:,4:6)));
t = all(M(:,1:2) >= -70 & M(:,1:2) <= 70,2);
exyz(t,:) = M(t,4:6);
ADD
so?
exyz2 = zeros(size(M(:,4:6)).*[1 2]);
t = all(M(:,1:2) >= 70 & M(:,1:2) <= 70,2);
exyz2(:,1:3) = M(:,4:6);
exyz2(t,4:6) = M(t,4:6);
ADD2
out = M;
t = all(M(:,1:2) >= 70 & M(:,1:2) <= 70,2);
out(t,4:6) = out(t,4:6);

Jeroen
Jeroen 2012 年 2 月 17 日
Thank Andrei Bobrov for your fast answer. But this yields a 235238 by 3 matrix and it needs to be a 235238 by 6 matrix. Anyway with your tips I can probably do something
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 2 月 17 日
in our case:
exyz2 == [ex2 ey2 ez2]

Jeroen
Jeroen 2012 年 2 月 17 日
Okay thank you for your respons! But this is not what i want. i have matrix M. That matrix has 6 rows and 235238 columns. Now if there is a value in that matrix that is below -70 or above 70 that element has to be zero.
The matrix that i create looks like: M = [M(:,1),M(:,2),M(:,3),ex2,ey2,ez2]; With ex2,ey2,ez2 the values that i have in the for loop.
Sorry for the late respons. I did not got a mail.
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 2 月 17 日
see ADD2 in my answer

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by