how to use the row&column indices returned by find to control array element in matrix way

2 ビュー (過去 30 日間)
I have a logical matrix (X) that are generated when the element meets certain conditions in the original raw matrix (data) and use "find" function to find the row and column indices for those elements. After that, I would like to achieve some operations on the neighbour elements of the original matrix. But I can't achieve it in the matrix method. Could anyone please give me some suggestions? The issue is illustrated as below:
For example:
[Y,X]=meshgrid(y_1grid,x_1grid);
% X size is (ie,je)
X= (X.^2 + Y.^2) <= (diam/2.0)^2 ;
% for instance, ie=7, je=7
X=[ 0 0 0 0 0 0 0;
0 0 0 1 0 0 0;
0 0 1 1 1 0 0;
0 1 1 1 1 1 0;
0 0 1 1 1 0 0;
0 0 0 1 0 0 0;
0 0 0 0 0 0 0];
[row, col]=find(X==1);
% ex and data are same size as X
ex(row,col)=ex(row,col)+0.5*(data(row,col)-data(row,col-1)); % this is wrong
% correct way, but the efficiency is too slow
for ii=1: ie
for jj=2:je
if ismeber (ii,row) && ismember (jj,col)
ex(ii,jj)=ex(ii,jj)+0.5*(data(ii,jj)-data(ii,jj-1);
else
ex(ii,jj)=ex(ii,jj)+0.1*(data(ii,jj+1)-data(ii,jj);
end

採用された回答

Shubham Gupta
Shubham Gupta 2019 年 8 月 8 日
編集済み: Shubham Gupta 2019 年 8 月 8 日
One of the way could be to use absolute indexing of X matrix :
ind = find(X==1);
ex(ind)=ex(ind)+0.5*(data(ind)-data(ind-je)); % je = size(X,1);
I hope it helps !
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 8 月 8 日
You compute ind but do not use it ?
Shubham Gupta
Shubham Gupta 2019 年 8 月 8 日
I edited my mistake, thanks !

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

その他の回答 (1 件)

Jiali
Jiali 2019 年 8 月 16 日
I appreciate your quick help. It really helps me to solve the whole issue.

カテゴリ

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

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by