Function won't index properly
조회 수: 13 (최근 30일)
이전 댓글 표시
I have created the following function to find the index numbers of the elements of array a that are less than or equal to b, whether be is a scalar or another array of length(a)=length(b).
function [m,k]=my_find(a,b)
for i=1:length(a)
if length(b)==1
k(i)=(a(i)<=b);
if k==1
m=i;
end
elseif length(b)==length(a)
k(i)=(a(i)<=b(i));
if k==1
m=i;
end
else
disp('Error')
end
end
disp('m = '),disp(m)
disp('k = '),disp(k)
For some reason, the m value does not index properly, even though the k value returns the proper values. The m value if supposed to replace the use of the find function. Any suggestions?
댓글 수: 0
채택된 답변
Andrei Bobrov
2011년 11월 6일
function [m, k] = Ammar_find(a,b)
na = numel(a);
nb = numel(b);
if nb ~= na && nb > 1,
disp('size "a" and "b" are not consistent');
m = [];
k = [];
else
k = a <= b;
m = nonzeros(k.*reshape(1:na,size(a)));
end
variant
function [m, k] = Ammar_find(a,b)
try
k = a <= b;
m = nonzeros(k.*reshape(1:na,size(a)));
catch err
rethrow(err);
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!