How to plot x,y, and intensity with NaN values present in the intensity data?
4 次查看(过去 30 天)
显示 更早的评论
Here is my code for my attempt to plot x, y, and intensity (referred to as v) on a color plot. I keep running into an issue where I cannot find the maximum of intensity due to NaN's present. Any advice to mitigate this issue or help refining this code is welcome. Thanks!
%plotting script
% Make an image of the points, then apply a colormap
x = x(:); % Extract x value
y = y(:); % Extract y value
v = v(:); % Extract intensity value
%find max and min values
minX = min(x,[],1);
maxX = max(x,[],1);
minY = min(y,[],1);
maxY = max(y,[],1);
[minV,~] = nanmin(v);
[maxV,~] = nanmax(v);
% Make image be 500 by 700
rows = 500;
columns = 700;
grayImage = zeros(rows, columns, 'uint8');
for k = 1 : length(x)
row = ceil((y(k) - minY) * rows / (maxY - minY));
column = ceil((x(k) - minX) * columns / (maxX - minX));
if (row == 0)
row = 1;
end
if (column == 0)
column = 1;
end
grayImage(row, column) = uint8(255 * (v(k) - minV) / (maxV - minV));
end
imshow(grayImage);
colorbar;
% Colormap is not gray scale.
% Apply some other colormap if you want
colormap(jet(256));
1 个评论
Walter Roberson
2017-9-25
You appear to me to be properly using nanmin() and nanmax().
The only thing I see is that you have been a little lax about handling v(k) being nan. Your calculation will result in uint8(nan) which turns out to be 0. If that is the result you want then I recommend adding a comment about that.
Also, the calculations (maxY - minY) and (maxV - minV) can be done ahead of time and stored in variables instead of being done every iteration. Also watch out for the possibility of constant input, in which case (maxV - minV) would be 0.
回答(1 个)
Image Analyst
2017-9-25
编辑:Image Analyst
2017-9-25
I don't see any plotting going on. Anyway, to extract only non-NaN values from an array, such as grayImage or v or whatever, do this:
goodValues = v(~isnan(v));
Then you can get the max, min, mean, etc. of those good values.
6 个评论
Image Analyst
2017-9-25
编辑:Image Analyst
2017-9-25
We don't have your v so can't even run your code. So I quit at that point. Can you attach v in a .mat file, so we can try to help you?
Walter Roberson
2017-9-26
In my test, I do not get index out of range.
Note: to get your color right, you should do
cmap = jet(256);
imshow(grayImage, cmap);
colorbar();
without calling colormap() after that.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Blue 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!