Values in column bigger than a variable?

2 views (last 30 days)
TYo
TYo on 25 Mar 2015
Edited: TYo on 25 Mar 2015
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

Accepted Answer

Guillaume
Guillaume on 25 Mar 2015
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 Comments
TYo
TYo on 25 Mar 2015
Edited: TYo on 25 Mar 2015
Thanks for your quick answer.
Guillaume
Guillaume on 25 Mar 2015
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);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!