Output showing used cells?

1 view (last 30 days)
Robert
Robert on 10 Dec 2014
Answered: Geoff Hayes on 10 Dec 2014
I have a condition in my code basically stating only use rows with data in each cell (if there is a NAN, don't use that row). It work fine, but I want it to generate some sort of list to tell me which rows it has used, e.g. if the data looked like
1 2 3 5
2 4 NAN 8
3 5 6 7
I need something like
True
False
True
Here is the part of my code that is relevant, what should I add?
% screen the data over all pixels to select the ones with good data for
% regression analysis
[rows, cols]=size(Amp);
data=zeros(1, 9); % Amp, PET, P, AI, NDVI, ELE, SLOPE, FLOWACCU, SPI
dip=0;
for i=1:rows
for j=1:cols
camp=Amp(i,j);
cpet=PET(i,j);
cp=P(i,j);
cai=AI(i,j);
cndvi=NDVI(i,j);
cele=ELE(i,j);
cslope=SLOPE(i,j);
cflow=FLOW(i,j);
cspi=SPI(i,j);
if isnan(camp)==0 && isnan(cpet)==0 && isnan(cp)==0 && isnan(cai)==0 && isnan(cndvi)==0 && isnan(cele)==0 && isnan(cslope)==0 && isnan(cflow)==0 && isnan(cspi)==0
dip=dip+1;
data(dip,:)=[camp, cpet, cp, cai, cndvi, cele, cslope, cflow, cspi]
end
end
end

Answers (1)

Geoff Hayes
Geoff Hayes on 10 Dec 2014
Robert - create a logical matrix outside of the for loop and update it at each iteration according to whether there is a NaN in the column (for that row) or not. Something like
% screen the data over all pixels to select the ones with good data for
% regression analysis
[rows, cols]=size(Amp);
data=zeros(1, 9); % Amp, PET, P, AI, NDVI, ELE, SLOPE, FLOWACCU, SPI
dip=0;
isNanFree = logical(zeros(rows,cols));
for i=1:rows
for j=1:cols
% etc.
if isnan(camp)==0 && ...
isNanFree(i,j) = true;
% etc.
end
end
You can then simplify the isNanFree matrix to a column vector if needed.
As an aside, you should try to avoid using i and j as names for (indexing) variables as MATLAB also uses i and j to represent the imaginary number.
Also, you could modify your conditions to something like
~isnan(camp)
which is equivalent to yours but a little neater.

Tags

Community Treasure Hunt

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

Start Hunting!