For loops and the find function
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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);
0 comentarios
Respuestas (1)
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().
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!