필터 지우기
필터 지우기

What is wrong on my script?

조회 수: 1 (최근 30일)
motevalizadeh
motevalizadeh 2017년 5월 10일
댓글: motevalizadeh 2017년 5월 11일
I'm new on matlab, and sorry if my question is simple :)
I have a function and call it on my main script
function [ f ] = fitness( x )
f =x(2);
end
My main script:
clc;
clear all;
close all;
fl=fitness( 2 );
display(fl);
and i have got this error:
Index exceeds matrix dimensions.
Error in fitness (line 3)
f=x(2);
What is wrong?

채택된 답변

Stephen23
Stephen23 2017년 5월 10일
편집: Stephen23 2017년 5월 10일
Inside your function fitness you obtain the second element of x using this line of code:
f =x(2);
but when you call your function fitness you only provide a 1x1 input, so there is no second element of x (when x is scalar it has only one element, so it is an error to try and get the second element: it does not exist!). Note that the number 2 is a scalar array (i.e. 1x1 array), so it has no second element:
fl=fitness( 2 );
You do not explain what you are trying to do, nor what you expect to happen, so it is hard to give any advice on how to fix this. One option is to provide an input with atleast two elements:
fl=fitness([1,3,5]);
but this might not be what you intended. Note that these very basic concepts, such as different array sizes, how to access their elements, and how to call functions are introduced in the Introductory Tutorials, which are highly recommended for all beginners:
You might like to read this as well, for some tips and hints for the future:
  댓글 수: 1
motevalizadeh
motevalizadeh 2017년 5월 11일
Thank u so much :)

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by