How do we assign NaN to multiple table elements most efficiently? Why can't we assign multiple columns at once?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I thought the following code would work to replace specific 0's with NaN in my data table. (Data was not recorded for these treatment fractions.)
ErrorData = ...
find(elektapatients.X==0&elektapatients.Y==0&elektapatients.Z==0);
[elektapatients(ErrorData,:).X elektapatients(ErrorData,:).Y...
elektapatients(ErrorData,:).Z]=NaN(length(ErrorData),3);
Instead this results in the error:
Error using NaN
Too many output arguments.
and I must instead assign each column separately:
elektapatients(ErrorData,:).X=NaN(length(ErrorData),1);
elektapatients(ErrorData,:).Y=NaN(length(ErrorData),1);
elektapatients(ErrorData,:).Z=NaN(length(ErrorData),1);
Is this the most efficient way? Why didn't MATLAB parse the previous command as 1st column to 1st column, 2nd column to 2nd column, 3rd column to 3rd column?
0 commentaires
Réponses (2)
the cyclist
le 28 Fév 2017
It's the same reason that this does not work:
x = zeros(5,2);
y = zeros(5,2);
z = rand(5,4);
[x, y] = z;
Even though it is "obvious" (to you) that you mean that x should be assigned the first two columns of z, and y should be assigned the other two, it is actually impossible for MATLAB to know that that is what is intended in general. Regardless of the prior dimensions of x and y, MATLAB cannot know that the programmer did not intend for the new x to be 5x3, and the new y to be 5x1.
2 commentaires
the cyclist
le 28 Fév 2017
Well, I'm not sure I would impugn all of MATLAB, based on one case in which the syntax did not do what you expect. All languages have syntax constraints that make sense in the broader context of the language.
In this case, my impression is that you have tried to shift your thinking back-and-forth from vectors to a matrix, when you could have been in a matrix from the beginning. The problem is actually upstream. Specifically, why do need
elektapatients.X
elektapatients.Y
elektapatients.Z
as separate column vectors at all? Why not a single matrix
elektapatients.XYZ
where I hope it is obvious what I mean. Then, you would have been able to do the natural
elektapatients(ErrorData,:).XYZ] = NaN(length(ErrorData),3);
(or something like that) in one fell swoop.
Instead, you are asking the language to be able to distinguish, syntactically, among
- an Nx3 matrix
- the concatenation of Nx2 and Nx1
- the concatenation of Nx1 and Nx2
- the concatenation of Nx1 and Nx1 and Nx1
and it can get even worse if you realize that maybe you actually had both column and row variables on the left. That's a lot to ask of the language.
Peter Perkins
le 28 Fév 2017
If you're putting NaNs into the same rows of X, Y, and Z, this should do what you want:
elektapatients{errorData,{'X' 'Y' 'Z'}} = NaN % braces, not parens
Hope this helps.
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!