How to reverse logical indexing?

3 vues (au cours des 30 derniers jours)
June Yang
June Yang le 17 Juil 2017
Modifié(e) : Stephen23 le 17 Juil 2017
Hi, let's say I have a data set A with NaN values that is a 3-D array.
B = ~isnan(A); AA = A(B);
Unfortunately now AA is a one-dimensional vector. How do I regain 3-D array from AA? There is a reshape function AAA = reshape(AA, X, Y, Z); but I cannot use this function because many elements got lost from logical indexing.
  1 commentaire
Stephen23
Stephen23 le 17 Juil 2017
Modifié(e) : Stephen23 le 17 Juil 2017
@June Yang: can you please tell what should happen in this example:
A = [1, 2
3, 999];
Z = A(A<100)
What shape should Z be? If you think it should be square, what value should replace the 999?
Z = [1, 2
3, ???];

Connectez-vous pour commenter.

Réponses (2)

John D'Errico
John D'Errico le 17 Juil 2017
You cannot, based on only what remains after the operation. You threw away information. It is now gone. Therefore the result is returned as a column vector.
A = [1 NaN; 2 3];
B = ~isnan(A)
B =
2×2 logical array
1 0
1 1
AA = A(B)
AA =
1
2
3
Arrays in MATLAB MUST always be rectangular. One row cannot have more elements than another row.
The are somethings you could do, if perhaps you wanted to just replace the NaNs with zeros, or perhaps inf, that would be easy. But there are limits, and irregular arrays fall beyond those limits.

Image Analyst
Image Analyst le 17 Juil 2017
You can't regain it from AA, but no problem - you still have A, so what's the problem?

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by