Values in column bigger than a variable?
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
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);
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!