Values in column bigger than a variable?

1 次查看(过去 30 天)
Hi all I have a Data.xls file converted into a matrix and I would like to compare the values in the first column of it with two numbers A and B, which change at a click of the user... So I already made everything except for how to make it compare the entire column 1 with the number A or B and give an error message if there is a value bigger than that.
This is a working code but unfortunately it takes the entire Data matrix and scans it , instead of just the first column.
for i=1:length(Data)
if Data(i)>=B
M=[M Data(i)]; % concatenating
msgbox(cat(2,{'Error message. Value bigger than:'} , num2str(B)),'Error'
end
end
else
N=[];
for i=1:length(Data)
if Data(i)>=A
N=[N Data(i)]; % concatenating
msgbox(cat(2,{'Error message. Value bigger than:'} , num2str(A)),'Error')
end
end
p.s. I would ideally like to show the user the ''address'' of the number with the value bigger than A, B.
Thanks

采纳的回答

Guillaume
Guillaume 2015-3-25
Using a loop for this is extremely inefficient. You can directly completely a matrix to a scalar It's simply:
aboveA = find(data > A);
if ~isempty(aboveA)
msgbox(sprintf('Values at index %d are bigger than %d', aboveA, A), 'Error');
end
%same with B
  2 个评论
TYo
TYo 2015-3-25
编辑:TYo 2015-3-25
Thanks for your quick answer.
Guillaume
Guillaume 2015-3-25
First column or whole matrix does not matter. There is never a need for a loop when comparing a matrix / vector / part of either with a scalar.
For just the first column:
aboveA = find(data(:, 1) > A);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by