필터 지우기
필터 지우기

why the error is coming?

조회 수: 2 (최근 30일)
VIJENDRA
VIJENDRA 2014년 6월 24일
댓글: Alfonso Nieto-Castanon 2014년 6월 24일
Trying to execute this code but it shows error "Subscript indices must either be real positive integers or logicals."
[M,N]=size(x); y=zeros(1,N); for l=1:N sum1=sum(x(:,l).*log(x(:,l))); sum1=-1*sum1; y(1,l)=sum1; end

채택된 답변

David Sanchez
David Sanchez 2014년 6월 24일
who is x?
That message is the result of trying to access an non-existent element of the array, such as x(0).
Make sure x has the right size.
By the way, your for-loop code
x= rand(3,4); % sample data
[M,N]=size(x);
y=zeros(1,N);
for l=1:N
sum1=sum(x(:,l).*log(x(:,l)));
sum1=-1*sum1;
y(1,l)=sum1;
end
Is the same that this, in a single line:
for l=1:N
y(l) = -( sum(x(:,l).*log(x(:,l))) );
end
  댓글 수: 1
VIJENDRA
VIJENDRA 2014년 6월 24일
x is a matrix and still showing this same error.

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2014년 6월 24일
편집: Roger Stafford 2014년 6월 24일
It is likely that somewhere in your system you have defined a variable named either 'sum', 'log', 'size', or 'zeros'. If so, this would be in conflict with one of matlab's functions with the same name and this would give rise to the error message you are receiving.
You can also write it as:
y = sum(-x.*log(x),1);
  댓글 수: 1
Alfonso Nieto-Castanon
Alfonso Nieto-Castanon 2014년 6월 24일
my money is on sum being a variable, given the choice of variable names used in the example...
clear sum;
should fix this issue...

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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by