How can I extract matrix without NaN values
Show older comments
I have two given matrices A and B in same size 5 x 5
A=[1 3 0 3 5; 2 3 4 1 0; 0 1 3 0 1; 2 4 5 4 4; 5 5 2 0 1]
B=[NaN NaN NaN NaN NaN; 2 3 4 1 NaN; 1 2 2 1 NaN; NaN 4 5 4 4; NaN 4 2 0 0]
How I could extract two sub-matrices of them - let's call them A1 and B1; by that B1 contains only numeric values of B, and A1 contains the corresponded values of A with same allocation index with numeric values of B. They should be like this
A1=[2 3 4 1; 0 1 3 0; 4 5 4 4; 5 2 0 1]
B1=[2 3 4 1; 1 2 2 1; 4 5 4 4; 4 2 0 0]
I tried for loops for a simple solution but it messed up. Thank you for your help~
Accepted Answer
More Answers (1)
A1=A.'; B1=B.'; % so can operate by row instead column...
isf=isfinite(B1);
>> B1=reshape(B1(isfinite(B1)),4,[]).'
B1 =
2 3 4 1
1 2 2 1
4 5 4 4
4 2 0 0
>> A1=reshape(A1(isf),4,[]).'
A1 =
2 3 4 1
0 1 3 0
4 5 4 4
5 2 0 1
>>
1 Comment
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!