Values in column bigger than a variable?

1 vue (au cours des 30 derniers jours)
TYo
TYo le 25 Mar 2015
Modifié(e) : TYo le 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

Réponse acceptée

Guillaume
Guillaume le 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 commentaires
TYo
TYo le 25 Mar 2015
Modifié(e) : TYo le 25 Mar 2015
Thanks for your quick answer.
Guillaume
Guillaume le 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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by