# How to remove zeros using Function in Matlab?

3 visualizzazioni (ultimi 30 giorni)
reyadh Albarakat il 24 Ago 2016
Risposto: Guillaume il 24 Ago 2016
Hi Everybody,
This 1st time I used a Matlab Function, I used it to read two images (Tiff format), one of them has NaN values and 2nd image has Zeros. I want to remove NaN and Zeros rows completely. By the way the output of 1st image is fine but the problem in the output of 2nd image. I have attached the Function and Script.
if true
%%%out_image ::: this is the name of your out image
%%%input_image ::: this is the name of your input image
%%%fname ::: dir name
image_reshape=reshape(image,1,size(image,1)*size(image,2));
image_t=image_reshape';
out_image=image_t;
out_image(find(out_image(:)<=no_data)) = NaN;
out_image=out_image(~isnan(out_image(:)),:);
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Guillaume il 24 Ago 2016
Considering that the nans you want to remove are the ones you put there on the previous lines, why put the nans in the first place?
To remove a row where any of the value on the row satisfy a condition, you would use the any function, so:
out_image(any(isnan(out_image), 2), :) = []; %remove rows that contain any nan
But as said, there's no point putting the nans there in the first place, so:
%replace the last three lines:
out_image = image_t(all(image_t > no_data, 2)); %only keeps rows of image_t whose values are ALL greater than no_data
Finally, note that the find is completely unnecessary in the code you wrote:
out_image(out_image <= no_data) = NaN;
%works exactly the same as
out_image(find(out_image <= no_data)) = NaN
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Contrast Adjustment in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by