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

2 views (last 30 days)
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

Accepted Answer

Shubham Gupta
Shubham Gupta on 8 Aug 2019
Edited: Shubham Gupta on 8 Aug 2019
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 !

More Answers (1)

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

Products


Release

R2015a

Community Treasure Hunt

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

Start Hunting!