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