How do I convert the weird magnitudes in the results to zero ?

Hi,
When I run the below program, I got some "NaN" in the results. So, I want to convert those "NaN" to be "Zeros" and I think the way to do it is using the for-loop but I am not sure how to do it due to the meshgrid, any idea please?
"
[x,y] = meshgrid(-5: .125: 5, -5: .125: 5);
x1 = -1;
y1 = 0;
V_x1 = ((y-y1)./((x-x1).^2+(y-y1).^2));
V_x1 = V_x1 (:)
fid = fopen('outputData.txt','wt');
fprintf(fid,'%.8f\n',V_x1);
fclose(fid);
"

回答 (1 件)

Mischa Kim
Mischa Kim 2014 年 5 月 6 日
編集済み: Mischa Kim 2014 年 5 月 6 日

1 投票

JMS, use something like
data = [1 2 NaN; 3 NaN 4];
data(isnan(data)) = 0;

3 件のコメント

JMS
JMS 2014 年 5 月 6 日
Yours might work for very short data length but when you have a lot of them, how do you specify at which point you need to convert them?
I think I need to do something like
"
ones = find(V_x1==NaN)
V_x1(ones)=0
"
But it doesn't work! It doesn't recognize 'NaN'
Mischa Kim
Mischa Kim 2014 年 5 月 6 日
編集済み: Mischa Kim 2014 年 5 月 6 日
Not quite sure I understand...
data = randi(2,10^1) % create 10-by-10 with 1s and 2s
data(data==2) = NaN % replace 2s by NaNs
tic
data(isnan(data)) = 0 % replace NaNs by 0s
toc % ...computation time
This approach will also work for larger matrices.
Try increasing the number of elements to 10^2 or 10^3 and don't forget to add semi-colons for larger matrices.
JMS
JMS 2014 年 5 月 6 日
Now it does work like
find_NaN = find(isnan(V_x1))
ones = (find_NaN)
V_x1(ones) = 0;
Thanks Mischa Kim!

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

カテゴリ

ヘルプ センター および File ExchangeInterpolation of 2-D Selections in 3-D Grids についてさらに検索

質問済み:

JMS
2014 年 5 月 6 日

編集済み:

2014 年 5 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by