For loops and the find function

조회 수: 3 (최근 30일)
Will
Will 2014년 11월 2일
댓글: Will 2014년 11월 2일
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);

답변 (1개)

Image Analyst
Image Analyst 2014년 11월 2일
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
Will
Will 2014년 11월 2일
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!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by