Reputation: 625
Initially, I have
Matrix A=
[ 1 2 3
4 255 6
NaN NaN NaN
7 8 9
10 11 12
NaN NaN NaN
10 9 11 ];
I find out the row numbers which are all NaN.
Row_NaN_MatA = [3 6];
After eliminating these rows, I am left with:
Matrix B1 =
[ 1 2 3
4 255 6
7 8 9
10 11 12
10 9 11 ];
After applying a filter, I make the second row of Matrix B = NaN NaN NaN. Therefore
Matrix B2 =
[ 1 2 3
NaN NaN NaN
7 8 9
10 11 12
10 9 11 ];
Now, the question is, after all these processing, I want to get the initial matrix back, but with all the deleted elements as NaN. So the required output I want is:
Output Matrix=
[ 1 2 3
NaN NaN NaN
NaN NaN NaN
7 8 9
10 11 12
NaN NaN NaN
10 9 11 ];
I know the dimensions of output I want (= initial Matrix A dimensions), and the row numbers which should be NaN (= Row_NaN_MatA) . The rest of the rows should be equal to rows of Matrix B2.
How can I do this?
Upvotes: 2
Views: 64
Reputation: 221504
Use setdiff
to get the row IDs that were not part of Row_NaN_MatA
by setdiff-ing
Row_NaN_MatA
with the a 1D array of indices for the entire row extent of A
, like so -
output = A
output(setdiff(1:size(A,1),Row_NaN_MatA),:) = B2
You can also use ismember
for the same effect -
output(~ismember(1:size(A,1),Row_NaN_MatA),:) = B2
Or use bsxfun
there -
output(all(bsxfun(@ne,Row_NaN_MatA(:),1:size(A,1))),:) = B2
Sample run -
>> A
A =
1 2 3
4 255 6
NaN NaN NaN
7 8 9
10 11 12
NaN NaN NaN
10 9 11
>> B1
B1 =
1 2 3
4 255 6
7 8 9
10 11 12
10 9 11
>> B2
B2 =
1 2 3
NaN NaN NaN
7 8 9
10 11 12
10 9 11
>> output
output =
1 2 3
NaN NaN NaN
NaN NaN NaN
7 8 9
10 11 12
NaN NaN NaN
10 9 11
Upvotes: 3