How to assign NaN in matrix by masking ?
Mostra commenti meno recenti
Hello everyone,
I have two matrices, matrix1 is three dimensional 180 x 360 x 12 (latitude x longitute x months) and matrix2 is two dimensional 180 x 360
In matrix one I want to assign NaN in entire matrix excluding some columns and some rows. column and row number found from second matrix using following code:
% mask = 100;
% [x y] = find(matrix2 == mask); %%%%by this I found the x and y coordinates.
Now how can I assign NaN in entire matrix1 excluding x and y coordinates which is found by matrix2 ? Can anyone help in this regard? I'll be thankful.
Risposta accettata
Più risposte (2)
Image Analyst
il 27 Lug 2016
Have you tried just standard masking like this with bsxfun():
% Mask the image using bsxfun() function
masked3DArray = bsxfun(@times, image3d, cast(mask2d, 'like', image3d));
You should get nan's in masked3DArray everywhere that mask2d had a nan. And where mask2d did not have a nan, i.e. where mask2d had a 1, masked3DArray should just be the original (1 times the image3d value).
It might be easiest in this case to instantiate a new 180 x 360 x 12 matrix of all NaNs, and just replace the vector at the appropriate row and column with the corresponding vector from the original matrix:
newMatrix = NaN(180,360,12);
newMatrix(x,y,:) = matrix1(x,y,:);
matrix1 = newMatrix
2 Commenti
Paxorus Sahay
il 26 Lug 2016
I think the OP expects more than one occurrence of mask in matrix2. With the solution you have suggested, the OP would have to use find(), then a for-loop through the matches, which would be un-MATLABish.
NathanM
il 26 Lug 2016
Ah, good catch.
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!