Extracting data from from all rows aside from those that contain NaN?
35 次查看(过去 30 天)
显示 更早的评论
How would I go about extracting data from one column based on the criteria of the other column?
For example, in column A, I have a few NaN values, after setting up my own criteria for this column. Column B has a its own values from randn values.
I now want to calculate the average of column B, but only rows that have a corresponding value in column A. Thus, if any value in column B corresponds to a NaN value in column A, I want to exclude that value from my average.
I'm tinkering around with the find function, but no luck yet.
0 个评论
采纳的回答
Cedric
2013-10-8
编辑:Cedric
2013-10-8
select = ~isnan( A ) ; % Vector of logicals, true at location corresponding
% to non-NaN elements of A.
values = B(select) ; % Logical indexing of B => extract elements of B
% which correspond to non-NaN elements of A.
Then you apply whatever you want to values. Note that there are functions which can make this selection for you, e.g. NANMEAN from the Stat. toolbox.
If you need these values for a one shot operation (which means if you don't have to reuse them), you can skip intermediary variables/steps. For example, fr sum these elements with a one-liner, you can do
sum( B(~isnan(A)) )
6 个评论
Cedric
2013-10-8
编辑:Cedric
2013-10-8
Most of what you say is correct, and you spotted a typo! :-) You are right, I should have written either
lid = isnan( Z(:,1) ) ;
values = Z(~lid,2) ;
theMean = mean( values ) ;
or
lid = ~isnan( Z(:,1) ) ;
values = Z(lid,2) ;
theMean = mean( values ) ;
As you explain, both are somehow the lengthy way to perform the same thing as the more compact
theMean = mean( Z(~isnan(Z(:,1)), 2) ) ;
MATLAB evaluates the most internal expression, and pipes the output in its container (don't know if this "pipe" analogy will help), and so on. When the large expression above is evaluated, MATLAB does the following roughly..
Evaluate: Z(:,1) (innermost)
|
| put the result in (or pipe)
v
Evaluate isnan( )
|
| put the result in (or pipe)
v
Evaluate ~( )
|
| put the result in (or pipe)
v
Evalaute Z( , 2)
|
| put the result in (or pipe)
v
Evaluate mean( ) (outermost)
Internally, MATLAB computes inner expression and creates intermediary arrays with results, that it passes to outer expressions. Now if you think that it makes the code clearer to make these intermediary steps by yourself and build your own intermediary arrays, you are free to do so. This is what I did when I built these examples with intermediary steps. The second reason for creating intermediary variables by yourself is when some intermediary variable could be reused elsewhere. Say, for example, that you have A with NaNs, and you want to address B, C, D, .., Z at locations where A is not NaN. You can do something like
meanB = mean( B(~isnan(A)) ) ;
meanC = mean( C(~isnan(A)) ) ;
meanD = mean( D(~isnan(A)) ) ;
..
but it is inefficent because ~isnan(A) is recomputed each time. In such case, it is more efficient to compute it once only and reuse it:
lid = ~isnan( A ) ;
meanB = mean( B(lid) ) ;
meanC = mean( C(lid) ) ;
meanD = mean( D(lid) ) ;
..
更多回答(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!