Using an empty matrix as an Index

조회 수: 6 (최근 30일)
Whitney
Whitney 2012년 12월 6일
I can't find any documentation about this so I don't understand why matlab doesn't consider using an empty matrix as an index an error worthy event.
x = [3,4];
y = find(x = 0); % returns an empty matrix
i(y) = 0; % returns i = []
Is there a reason for this? It took me quite a while to realize this was causing problems in my program and when I did discover the behavior, I had a momentary thought that I was using perl instead of matlab.

답변 (2개)

Sean de Wolski
Sean de Wolski 2012년 12월 6일
What do you expect it to return? If it's not being indexed by anything then it shouldn't extract anything. Perhaps this will look slightly better:
X = magic(5)
X(2:1,:)
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 12월 6일
Yes. It is a useful part of MATLAB.
Jan
Jan 2012년 12월 7일
Exactly. The posted code does this:
Create a variable "i", which have all elements of the index vector set to 0. If the index vector is empty, "i" must be empty also.

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


Pritesh Shah
Pritesh Shah 2012년 12월 7일
x = [3,4]; y = find(x = 0); % returns an empty matrix i(y) = 0; It should be == instead of = y = find(x == 0); % returns an empty matrix
Now, it will work... Enjoy !!

카테고리

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