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