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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by