How to search through Data in a Matrix to find a specific element using For Loops and if statements?

조회 수: 5 (최근 30일)
B

채택된 답변

ANKUR KUMAR
ANKUR KUMAR 2021년 3월 10일
편집: ANKUR KUMAR 2021년 3월 10일
Why do you wish to use for and if for searching the locations of SE? You can use without using for and if, and it would be very quick and short.
R12=randi(15,12,12)
SE=5
[xx,yy]=find(R12==SE);
[xx,yy]
But still if you wish to use for and if, below is the code you can use:
location=1;
for ii=1:size(R12,1)
for jj=1:size(R12,2)
index=R12(ii,jj)==SE;
if index==1
xx_index(location)=ii;
yy_index(location)=jj;
location=location+1;
end
end
end
[xx_index; yy_index]'
Hope this helps.
  댓글 수: 4
ANKUR KUMAR
ANKUR KUMAR 2021년 3월 11일
Yes, that is the logical part. The code gives you the x and y indices of the locations of SE elements in the R12 matrix.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by