Given the matrix A, return B in which all the rows that have one or more NaNs have been removed. So for
A = [ 1 5 8 -3 NaN 14 0 6 NaN ];
then
B = [ 1 5 8 ]
It's a good problem!
very good problem
ya.. but not so difficult though
nice trick with the " ' "))
very good problem
I really like this problem. It makes me to improve my solutions again and again. But it seems that I still use too many loops....
lol test case with transform was good one!
do not understand how one get so efficient solution on this (and to be honest other problems as well)
thanks you very much.
Why doesn't this work? It works when I use it on my computer...
function B = remove_nan_rows(A)
B = A;
B(any(isnan(A')),:) = [];
end
MATLABotic!
These types of problems are giving me so many useful functions
A(~sum(isnan(A),2),:) this workde for me
Cool!
Very good problem!
A easy problem if you know fuction isnan.
Nice one!
Oh
two NaNs are not equal to each other, using isnan to detect NaNs. Thus,
a = a(all(a==a,2),:);
a = a(~any(isnan(a),2),:);
a(any(a~=a,2),:) = [];
a(any(isnan(a),2),:) = [];
really cool!
I think that test 4 does not comply to the question.
A(~sum(isnan(A),2),:) this worked for me
O(^-^) o
function B = remove_nan_rows(A)
B = A;
B(any(isnan(A')),:) = [];
end
Why isn't this working?
I like the solution
a = a(all(a==a,2),:); % a = a(~any(isnan(a),2),:);
a(any(a~=a,2),:) = []; % a(any(isnan(a),2),:) = [];
来自知乎
If I can't view a smaller solution, how can I improve myself ??
I really want to view the better answers.
The only improvement you can really make is making it one line
B = A( not( any(isnan(A), 2 ), :)
really?
I really liked this problem. I know that I took a very long approach to it but in time I hope to improve it. I have had a lot of practice with for and while loops which is why I have taken this approach.
i am unable to compare it with inf. how can i do that??
Can anyone tell me why matrix B is not being returned correctly? If I put it one 'end' further into the loop it returns the matrix each time a row is taken out and the tests fail
when you delete one row of B, the size of B changes.
your solution works with an assumption that the size of B won't change.
2 points better than my previous solution with ~any(isnan(
Passes the defined tests, but would fail if Inf was in the test matrices.
Can anyone please tell me how to reduce this size?
You already use vectorized assignment. Carry that further and get rid of the for loop.
The functions sum() and prod() can reduce row-wise to bring out NaN values. Thus, this express produces logicals for whether each row is in or out...
~isnan(prod(A,2))
Can anyone tell me, how it can coded in size of 10???
Using regexp, you can put all the code into a character string, and the character string only has a size of 1.
Good one...
Would fail if test suite contained any Infs.
^ Yes, this happens to work on the test inputs, but is technically not doing what the problem asks.
On the other hand, B = A(all(A==A,2),:) works.
19675 Solvers
240 Solvers
Back to basics 8 - Matrix Diagonals
784 Solvers
Simple equation: Annual salary
4513 Solvers
ベクトル [1 2 3 4 5 6 7 8 9 10] の作成
263 Solvers