How can I repeat same process for different matrices?
1 次查看(过去 30 天)
显示 更早的评论
I have 8 timeseries from different tide gauges. The first column of these matrices is deciyear, the second column is sea level.
I want to define -99999 as NaN in the second columns (i.e. the sea level) for each matrices. I know how to do this in a single matrix:
e.g. for the variable ' Boston ':
Name Size Bytes Class Attributes
Boston 1176x4 37632 double
Sea_Level_of_Boston = Boston(:,2);
Sea_Level_of_Boston(Sea_Level_of_Boston == -99999)= NaN;
Boston(:,2) = Sea_Level_of_Boston;
However, I want to repeat this for all the timeseries (e.g. Bridgeport, Eastport). I am curious if there is a easy way for that?
PS: all the matrices have 4 columns but they have different length, i.e. the time span various.
I have done combining the matrices, but I really want to seperate them afterwards for plotting and analysing.
2 个评论
Stephen23
2019-9-21
This line will throw an error:
Sea_Level_of_Boston(Sea_Level_of_Boston = -99999)= NaN;
% ^ this is NOT an equivalence operator!
采纳的回答
Stephen23
2019-9-21
编辑:Stephen23
2019-9-21
Of course there is an easy way: just use Indexing! Indexing is simple and very efficient:
C = {boston,paris,beijing,...};
for k = 1:numel(C)
M = C{k};
X = M(:,2)==-99999;
M(X,2) = NaN;
C{k} = M;
end
Or simply avoid creating M altogether:
C = {boston,paris,beijing,...};
for k = 1:numel(C)
X = C{k}(:,2)==-99999;
C{k}(X,2) = NaN;
end
"but I really want to seperate them afterwards for plotting and analysing."
Also trivially easy using exactly the same indexing.
Note that putting meta-data into variable names, e.g. boston and Sea_Level_of_Boston, is a bad idea, because trying to access meta-data in variable names is one way that beginners force themselves into writing slow, complex, obfuscated, inefficient, buggy code that is difficult to debug.
Meta-data is data, and data should be stored in variables, not in variable names.
4 个评论
Stephen23
2019-9-21
"I shouldn't expect them in the previous variables."
Correct.
And in future, when designing your data, keep in mind that meta-data (e.g. city names) belongs in varables and not in variable names. Then your code will be simpler, more robust, and much more efficient.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!