For loops and the find function
Afficher commentaires plus anciens
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);
Réponses (1)
Image Analyst
le 2 Nov 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 commentaire
Will
le 2 Nov 2014
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!