Manipulation of certain matrix elements which meet a certain criterion

4 ビュー (過去 30 日間)
Praneeth Lanka
Praneeth Lanka 2021 年 8 月 30 日
コメント済み: KALYAN ACHARJYA 2021 年 8 月 30 日
Let's say I have a matrix of size 100X100. I wish to manipulate certain elements of the Matrix which are in Rows 10 to 15 and coulmns 11 to 13, which are negative numbers. I donot wish to change the elements which are negative outside the specified Rows and Columns.
Is it possible to do this without creating any new variable ? by using Logical Indexing?
Edit: I have generalized the question, but here is where I intend to implement the solution provided. I have a 15X16 matrix in which there are NaN values in random positions of the matrix. I would like to set these NaN values to 0s' in only the sub-matrix formed by Rows 2 to 15 and Columns 2 to 16. So in this case I would be required to use the isnan function.
  2 件のコメント
Wan Ji
Wan Ji 2021 年 8 月 30 日
I dont know what you mean. I respect your question
Praneeth Lanka
Praneeth Lanka 2021 年 8 月 30 日
Hey, I have edited the question. Please check if you could understand the problem now.
Thanks

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

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 8 月 30 日
mat=randi([-5,5],[100,100]); % Sample Matrix
[r,c]=find(mat(10:15,11:13)<0==1); % Get the indices
mat(9+r,10+c)=x; % x Manupulate any Value
Please note there may be more easier solution.
  2 件のコメント
Praneeth Lanka
Praneeth Lanka 2021 年 8 月 30 日
Thanks for this. I was aware of the find function, however I was thinking of some direct solution (if it exists), something like
mat(mat<0)
I know this would manipulate all the values, but I was wondering if there exists a way to write similar lines which would just manipulate the values in specified rows and columns.
I have generalized the question, but here is where I intend to implement the solution provided. I have a 15X16 matrix in which there are NaN values in random positions of the matrix. I would like to set these NaN values to 0s' in only the sub-matrix formed by Rows 2 to 15 and Columns 2 to 16. So in this case I would be required to use the isnan function.
Thanks again
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 8 月 30 日
Yes you can get that ...
idx=mat(10:15,11:13)<0;
But during manipulation, you may have to use groups of rows & columns number.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by