How can i pull a matrix out of matrix with indices?

1 回表示 (過去 30 日間)
Jiahong Zou
Jiahong Zou 2023 年 6 月 16 日
回答済み: Matt J 2023 年 6 月 16 日
Hi everyone. Sorry for the poor expression. I used find() function to find elements, which fulfill certain condition (non constant) . I want to have a matrix of same dimension, which contains the rest of elements in Matrix (constant) . Here is my code. But it runs incredibly long, because dimension of thisMatrix{1} is 30000 * 30000 big. How can i do this faster?
Mat_nonconst_tmp = sparse(size(thisMatrix{1}, 1), size(thisMatrix{1}, 2));
for n = 1:length(row_nonconst)
Mat_nonconst_tmp(row_nonconst(n), column_nonconst(n)) = thisMatrix{1}(row_nonconst(n), column_nonconst(n));
end
Mat_const = thisMatrix{1} - Mat_nonconst_tmp;

採用された回答

Matt J
Matt J 2023 年 6 月 16 日
Do it a completely different way. Example
A=rand(5)
A = 5×5
0.1956 0.2027 0.4764 0.3016 0.3337 0.6153 0.6513 0.2582 0.3525 0.6252 0.9584 0.8762 0.8876 0.8489 0.0939 0.3990 0.3480 0.6502 0.5263 0.3498 0.6580 0.5777 0.8174 0.1849 0.9899
B=rand(5)
B = 5×5
0.9819 0.5626 0.9978 0.3930 0.7500 0.3371 0.1098 0.4372 0.7146 0.8823 0.2073 0.8749 0.0540 0.1992 0.7296 0.1819 0.4807 0.9508 0.0666 0.0887 0.6023 0.3610 0.9348 0.6361 0.9436
condition=A<0.5;
B(condition)=0
B = 5×5
0 0 0 0 0 0.3371 0.1098 0 0 0.8823 0.2073 0.8749 0.0540 0.1992 0 0 0 0.9508 0.0666 0 0.6023 0.3610 0.9348 0 0.9436

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by