Matlab - Access index of max value in for loop and use it to remove values
from array
I would like to recursively find the maximum value in a series of matrices
(column 8, to be specific), then use the index of that maximum value to
set all values in the array with index up to the max index to NaN (for
columns 14:16). It is straight forward to find the max value and index,
but using a for loop to do it for multiple arrays I am stumped.
Here is how I can do it without a for loop:
[C,Max] = max(wy2000(:,8));
wy2000(1:Max,14:16) = NaN;
[C,Max] = max(wy2001(:,8));
wy2001(1:Max,14:16) = NaN;
[C,Max] = max(wy2002(:,8));
wy2002(1:Max,14:16) = NaN;
and so on and so forth...
Here are two ways I have tried using a for loop:
startyear = 2000;
endyear = 2009;
for n=startyear:endyear
currentYear = sprintf('wy%d',n);
[C,Max] = max(currentYear(:,8));
currentYear(1:Max,14:16) = NaN;
end
Here is another way I tried, using the eval function
for n=2000:2009;
currentYear = ['wy' int2str(n)];
var2 = ['maxswe' int2str(n)];
eval([var2 ' = max(currentYear(:,8))']);
end
In both cases, the problem seems to be that MATLAB doesn't recognize the
'currentYear' variable to be the array that corresponds to the wyXXXX that
I already have created in my workspace.
Please let me know how I can clarify my question if it doesn't make sense.
Thank you!
Matt
No comments:
Post a Comment