Values in column bigger than a variable?

1 visualización (últimos 30 días)
TYo
TYo el 25 de Mzo. de 2015
Editada: TYo el 25 de Mzo. de 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

Respuesta aceptada

Guillaume
Guillaume el 25 de Mzo. de 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 comentarios
TYo
TYo el 25 de Mzo. de 2015
Editada: TYo el 25 de Mzo. de 2015
Thanks for your quick answer.
Guillaume
Guillaume el 25 de Mzo. de 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);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by