For loops and the find function

3 visualizaciones (últimos 30 días)
Will
Will el 2 de Nov. de 2014
Comentada: Will el 2 de Nov. de 2014
For a homework assignment, I have to:
1. Use the find function to fin the column and row of all the values in a matrix that are greater than a user input.
2. Use a for loop to find the values in the matrix based off the row and column location.
I can do all of it, but the for loop is not working the way I want it to be.
Here is my code so far:
load('Refract.mat');
A = Refract;
MaxVal = input('Input the maximum refraction value: ');
while MaxVal < 0
disp('ERROR: The input must be greater than 0.');
MaxVal = input('Inpu the maximum refraction value: ');
end
[rows,columns,values] = find(A > MaxVal);
length = length(columns)
for x = [1:length]
Values = rows(x),columns(x);
end
Table = [rows,columns];
disp(' Rows Columns Value');
disp(Table);

Respuestas (1)

Image Analyst
Image Analyst el 2 de Nov. de 2014
Glad you at least tried something before just posting your homework. Don't use length as the name of a variable because it is the name of a built-in function. Try this:
numElements = length(columns)
for k = 1 : numElements
Values(k) = A(rows(k),columns(k));
fprintf('A(%d, %d) = %f\n', rows(k), columns(k), Values(k));
end
% Display in command window
Values
% No need for Table or disp().
  1 comentario
Will
Will el 2 de Nov. de 2014
My teacher wants it to be displayed like that (I know, very annoying)
But I modified what you put and got it to work out the way my teacher likes, so thank you so much!!

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB 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