Using logical index matrix to create another matrix with values

조회 수: 66 (최근 30일)
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

채택된 답변

Steven Lord
Steven Lord 2022년 8월 3일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by