フィルターのクリア

Replacement for for loops

1 回表示 (過去 30 日間)
Dhananjay Mishra
Dhananjay Mishra 2018 年 10 月 12 日
コメント済み: Star Strider 2018 年 10 月 12 日
Hello everyone. I want to construct an array of data which will contain values like below example. Any suggestions on how to do this without using for loop will be very helpful. Thank you!
A = zeros();
x1 = 1
for u = -1:1:1
for v = -1:1:1
A(x1) = exp((-1i*(u-v).^2)/2);
x1 = x1+1;
end
end

採用された回答

Star Strider
Star Strider 2018 年 10 月 12 日
Try this:
[U,V] = meshgrid(-1:1);
Am = exp((-1i*(U-V).^2)/2);
A = Am(:).';
It produces the same output as your code.
  2 件のコメント
Dhananjay Mishra
Dhananjay Mishra 2018 年 10 月 12 日
Many thanks for your answer. However how to remove for loop if I have a code like this: I am new to MATLAB hence having problems implementing the code
x1=1;y1=1;u1=1;v1=1;
for u = -1:1:1
for v = -1:1:1
for x = -1:1:1
for y = -1:1:1
value(x1,y1) = exp( ( -1i*(((u-x)^2) + (v-y)^2))
y1 = y1+1;
end
x1 = x1+1;
y1 = 1;
end
x1 = 1;
v1 = v1 + 1;
end
u1 = u1+1;
v1 = 1;
end
Star Strider
Star Strider 2018 年 10 月 12 日
As always, my pleasure.
I cannot understand what you want to do. If your code produces the result you want, just leave it as is. I doubt it can be vectorised.
Also, there are too many parentheses in your ‘value’ assignment, so it throws an error. I eliminated one left parenthesis to make it work:
value(x1,y1) = exp(-1i*((u-x)^2 + (v-y)^2))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by