필터 지우기
필터 지우기

Why wont it output index?

조회 수: 1 (최근 30일)
Hunter Steele
Hunter Steele 2019년 10월 8일
편집: Sulaymon Eshkabilov 2019년 10월 9일
function[max,index]=MyMax(x)
max=0;
[n,m]=size(x);
for i=1:m
if max<x(i)
max=x(i);
index=i;
output = [max, index];
end
end
end

채택된 답변

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 10월 9일
Hi,
You have done everything correctly except for a couple of minor flaws. Here is the corrected code:
function output = MyMax(x)
MAX=0; % It is better to avoid MATLAB's function names, such as, max, min, etc to name variables
[n, m]=size(x);
for i=1:m
if MAX<x(i)
MAX=x(i);
index=i;
output = [MAX, index];
end
end
end
This can be tested:
>> x=linspace(-pi,pi);
>> OUT = MyMax(x)
OUT =
1.0e+02 *
0.031415926535898 1.000000000000000
Note that the easy solution of your exercise is this simple command:
[MAX_VAL, INDEX] = max(x)
Good luck.
  댓글 수: 1
Hunter Steele
Hunter Steele 2019년 10월 9일
Thank you! What would i need to change to find minimum?

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

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019년 10월 9일
편집: Sulaymon Eshkabilov 2019년 10월 9일
Just use:
[MAX_VAL, INDEX] = min(x) % instead of max()
Good luck.
  댓글 수: 1
Hunter Steele
Hunter Steele 2019년 10월 9일
It seems to not be outputing anything.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by