Counting variables, and getting last column of matrix within specific criteria
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi everyone,
I have a problem in matlab. I want to take last column of a 12094*3 matrix within specific criteria. 12094*3 matrix has three columns which are lattiutude, longitude and magnitude in order. I have another matrix has two columns which are longitudes and lattitudes and this matrix is a closed region. In my code c is closed region matrix and all is 12094*3 matrix. If lattitudes and longitudes of c are same with all matrix, it should take last column which is magnitude M. However, when i run the code it gives wrong values. There seems like that there are seven 6.9418, but it is not.
True values are last colum and there is only one 6.9418.
I found this error for only 36.5 and 25.5, but i think there are more mistakes.
My code is:
for j=1:size(c)
for i=1:size(all)
if ((all(i,1)==c(j,1)) && (all(i,2)==c(j,2)))
M(j)=all(i,3);
else
end
end
end
댓글 수: 0
채택된 답변
Bob Thompson
2021년 3월 25일
I'm not sure I entirely understand the issue with 6.9418. Are you saying you only want 6.9418 as the magnitude, even though you have seven instances of the lat/long of (36.5, 25.5)?
As a quick aside, you shouldn't need the loops to perform the logic, you should just be able to use logic indexing and get it all in one go.
M = all(all(ismember(all(:,1:2),c,'rows'),3);
댓글 수: 3
Bob Thompson
2021년 3월 26일
I apologize, there's an extra 'all' because I changed my method half way through writing that.
M = all(ismember(all(:,1:2),c,'rows'),3);
I think MATLAB is giving you the same magnitude for the duplicate lat/long because of the indexing setup in your loops. The logic indexing should fix that, though you will end up with all seven magnitudes for the duplicates. If that's what you want, then this should be good. If you only want one entry for the max value then things get more complicated.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!