Using logical index matrix to create another matrix with values

91 views (last 30 days)
Hi All,
I have a matrix (E) with 180 x45 with values. I would like to create another matrix where the values are >1E9. First I found the indexes (idx) of the values that are >1E9 in matrix E. Now I would like to use that matrix index (idx) to create the another matrix (E2) that have the values that are >1E9. But it created just one column data insted not a matrix. How can I solve this?
Thank you
idx=E>1E9;
E2=E(idx);
Birsen

Accepted Answer

Steven Lord
Steven Lord on 3 Aug 2022
What do you want the elements that did not satisfy the condition in your original matrix to be in the new matrix? You can't have a "Swiss cheese" matrix (one with holes in it.)
Here's one example that fills in where those holes would have been with NaN values.
A = magic(4)
A = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
ind = A > 8
ind = 4×4 logical array
1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0
B = NaN(4)
B = 4×4
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
B(ind) = A(ind)
B = 4×4
16 NaN NaN 13 NaN 11 10 NaN 9 NaN NaN 12 NaN 14 15 NaN
Note that if you'd tried to use ind to extract just those elements of A, you are correct that you'd receive a vector. Remember, no holes allowed.
C = A(ind)
C = 8×1
16 9 11 14 10 15 13 12

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!