フィルターのクリア

How to replace elements in part of a matrix that meet a certain criteria?

26 ビュー (過去 30 日間)
Sobhan
Sobhan 2024 年 3 月 21 日
コメント済み: Sobhan 2024 年 3 月 21 日
I want to replace elements in certain rows and columns with 0, if they are greater or less than 0. As I am using a sparse matrix (that's 10 000 x 10 000) I don't think I can just set those rows equal to zero, so I am specifically looking for all nonzero elements and replacing them. I can do this row by row in a for loop over i: A( rows(i) , abs(A(rows(i),:))>0 )=0, if rows is a 1D array with row indices. I was wondering if there is a way to do this for the whole matrix without looping? I tried A( rows , abs(A(rows,:))>0 )=0, but that doesn't work as my logical array is 2D.

採用された回答

Hassaan
Hassaan 2024 年 3 月 21 日
編集済み: Hassaan 2024 年 3 月 21 日
% Assuming A is your sparse matrix and 'rows' is the 1D array of row indices you're interested in
[rowIdx, colIdx, values] = find(A);
% Initialize a logical vector to mark elements to change
toChange = false(size(values));
% Loop through the specific rows you're interested in
for i = 1:length(rows)
% Find indices in rowIdx that match the current row of interest
inRow = rowIdx == rows(i);
% Mark these for change (you can adjust this condition as needed)
toChange(inRow) = abs(values(inRow)) > 0;
end
% Now, toChange contains true for elements to be set to zero
% Replace values to be changed with 0
values(toChange) = 0;
% Create the new sparse matrix
A_new = sparse(rowIdx, colIdx, values, size(A,1), size(A,2));
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 件のコメント
Sobhan
Sobhan 2024 年 3 月 21 日
This is similar to what I did, but my question was more specificly if there is a way to do it without looping.
My original solution was:
bottomD = [3,5,7]; %example
K = rand(10,10); %example (although it's sparse)
for i = 1:length(bottomD)
K(bottomD(i),abs(K(bottomD(i),:))>0)=0;
end
I figured out that K(bottomD,:)=sparse(length(bottomD),10) works as I don't care about the values in those rows, so I can just replace the entire rows.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by