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 !

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by