% % 是在矩阵的每行里找到第一个小于3的点吗?这样写:
data = 10*rand(5,10);
[r,c] = size(data);
result = [];
for i = 1:r
tmp = data(i,:);
tmp = tmp(data(i,:)<3);
if length(tmp)
result(i,:) = tmp(1); % 如果有许多小于3的数,只要第一个
else
result(i,:) = NaN; % 如果没有小于3的数,存NaN
end
end
result
一、
data = 10*rand(5,10);
[r,c] = size(data);
result = [];
for i = 1:r
tmp = data(i,:);
tmp = tmp(data(i,:)<3);
if length(tmp)
result(i,:) = tmp(1); % 如果有许多小于3的数,只要第一个
else
result(i,:) = NaN; % 如果没有小于3的数,存NaN
end
end
result
二、% % 要把列号也存起来的话,稍微改一下就好了:
data = 10*rand(5,10);
[r,c] = size(data);
result = [];
result_c = [];
for i = 1:r
tmp = find(data(i,:)<3);
if length(tmp)
result_c(i,:) = tmp(1);
result(i,:) = data(i,tmp(1));
else
result_c(i,:) = NaN;
result(i,:) = NaN;
end
end
[result_c, result]