How can I repeat same process for different matrices?

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

This line will throw an error:
Sea_Level_of_Boston(Sea_Level_of_Boston = -99999)= NaN;
% ^ this is NOT an equivalence operator!
The equivalence operator in MATLAB is ==, aka eq.
Yes you are right, I have edited it.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
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

Hi, thank you for your reply, but some part of the code does not work:
I have tried
C = {boston,paris,beijing,...};
for k = 1:numel(C)
M = C{k};
X = M(:,2)==-99999;
M(X,2) = NaN;
C{k} = M;
end
M is constructed perfectly. When k = 1 (i.e. boston), then M is an array having the same dimention as boston, while having the values -99999 substituted to NaN.
However,
C{k} = M;
Does not work. Instead, I have tried
boston = M;
This works.
I guess there is a problem substituting the matrix using indexing? or more precisely, the substitution could go through to C{k} but fail to go back to the original matrix boston?
Stephen23
Stephen23 2019년 9월 21일
편집: Stephen23 2019년 9월 21일
"However, C{k} = M; Does not work."
Actually it works perfectly, in that the array M will be assigned to the k-th cell of C.
"Instead, I have tried boston = M; This works."
Sure, but only if you are happy overwriting the same variable on every loop iteration, thus making the loop entirely pointless. And if you really did want to write back to different matrices, then you get back to exactly the same problem that I showed you how to avoid (by using indexing).
"I guess there is a problem substituting the matrix using indexing?"
Nope, no problem there. Just your assumptions (about pointer references) are incorrect.
"or more precisely, the substitution could go through to C{k} but fail to go back to the original matrix boston?"
MATLAB does NOT pass values as pointers/references to data, nor does anything in the MATLAB documentation state that such a thing should occur, nor did I write that that would occur.
In my answer I told you exactly how to access your data: using indexing.
While you can continue to try and force MATLAB into how you imagine that it works (but doesn't), you would save a lot of time and effort by simply using indexing.
Ah I see. I had some misunderstanding. Finally the new values are saved in C. I shouldn't expect them in the previous variables. Thank you very much!
"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개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2019년 9월 21일

댓글:

2019년 9월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by