Question about min and if function
이전 댓글 표시
Dear All
I have a matrix of three column as below like the file provided, I would like to find the values from o or p columns which is the closest to q, I have written this code:
M=abs(o - q);
N=abs(p - q);
difference =[M N];
[~,ii] = min(difference,[],2);
if ii == 1
Result = o
else
Result = p
end
but it just return the p column to me, I am really confused about the results. I really appreciate your helps
Best Nima
댓글 수: 3
Michal
2017년 11월 14일
Add some test case data to your code and expected result.
if ii == 1
will only be true if all values of ii are equal to one, and by the way that you defined ii this is clearly not the case. That line does not operate element-wise on the elements of ii. You cannot use if to distinguish between different array values and cause different actions to happen without using a loop and indexing.
Even better would be to learn how to write vectorized code, in which case the loop is not required. These basic and important concepts for using MATLAB are explained in the Getting Started tutorials, which are highly recommended for all beginners:
What output do you expect to get?
Nima
2017년 11월 14일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
