Finding Certain Values In A Matrix

So here is what i need to do: I need to find a certain values in a matrix that are less than or greater than another value. I need to do this in an if statement. For example if my matrix is [1 2 3 4 5 6; 7 8 9 10 11 12]. I want to find all the values that are less than 4 and have an fprintf statement saying something. And then find all the values that are greater than 6 and have another fprintf statement. And then for all other values, have an fprintf statement. How can I do this?

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 8 de Mzo. de 2013
Editada: Azzi Abdelmalek el 8 de Mzo. de 2013
A=[1 2 3 4 5 6; 7 8 9 10 11 12]
idx1=find(A<4)
idx2=find(A>6)
out1=A(idx1)
out2=A(idx2)

3 comentarios

Ryan
Ryan el 8 de Mzo. de 2013
Editada: Ryan el 8 de Mzo. de 2013
thanks that helped, now is there a way that I can find the value in a matrix that is closest or for my purpose, within 5% of a certain value?
Azzi Abdelmalek
Azzi Abdelmalek el 8 de Mzo. de 2013
Editada: Azzi Abdelmalek el 8 de Mzo. de 2013
idx=find(abs((A-yourvalue)./A)<0.05)
out=A(idx)
To finish your questions:
fprintf('The values less than 4 are\n');
fprintf('%d, ', out1);
fprintf('\nThe values greater than 6 are\n');
fprintf('%d, ', out2);
Use %f instead of %d if you have floating point numbers instead of integers.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Mzo. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by