필터 지우기
필터 지우기

Find returns empty with inconsistent size

조회 수: 5 (최근 30일)
Rub Ron
Rub Ron 2021년 8월 26일
댓글: Walter Roberson 2021년 8월 26일
I have a variable x, that is a vector with a variable number of elements. When applying the function
find(x>0), for:
find([-1 -1 -1 -1 -1 -1]>0)
ans =
1×0 empty double row vector
find([-1 -1]>0)
ans =
1×0 empty double row vector
find([-1]>0)
ans =
[]
The problem I have is that when x has only one element, and the size of the output is not consistent. For instance, I would like to get an empty with the same size as "1×0 empty double row vector".
Any suggestions?

채택된 답변

Walter Roberson
Walter Roberson 2021년 8월 26일
편집: Walter Roberson 2021년 8월 26일
find([-1 -1 -1 -1 -1 -1]>0)
ans = 1×0 empty double row vector
find([-1 -1 -1 -1 -1 -1].'>0)
ans = 0×1 empty double column vector
find([-1 -1]>0)
ans = 1×0 empty double row vector
find([-1 -1].'>0)
ans = 0×1 empty double column vector
find([-1]>0)
ans = []
size(ans)
ans = 1×2
0 0
So when there is 1 row, the result has 0 columns. When there is 1 column, the result has 0 rows. When there is 1 row and 1 column (scalar), the result has 0 rows and 0 columns.
  댓글 수: 2
Rub Ron
Rub Ron 2021년 8월 26일
How can I make that when x is a scalar, my the empty output has is 1x0 size?
Walter Roberson
Walter Roberson 2021년 8월 26일
There are no options to find() that can make that behaviour happen. You can, however, do
idx = find([-1]>0);
if isempty(idx); idx = zeros(1,0); end

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

추가 답변 (1개)

Awais Saeed
Awais Saeed 2021년 8월 26일
편집: Awais Saeed 2021년 8월 26일
This command is for vectors and matrices. find(X > 0) returns the indices of the array X where elements are greater than zero. If none is found, it returns an empty matrix. Now using this command on scalers ( [-1] in your case) does not make any sense.
However, if you want 1×0 empty double row vector then you have to check whether X is a scaler or not. If X is a scaler, then do
X = -1:-2 % this will be an empty vector and will give you 1×0 empty double row vector
Use
isscalar()
yo check if X is a scaler ot not
  댓글 수: 3
Awais Saeed
Awais Saeed 2021년 8월 26일
편집: Awais Saeed 2021년 8월 26일
No there is not. To get 1x0 empty double row for scaler values, just check if they are scalers or not. If they are, then by doing
j : k % where j > k, will give you 1x0 empty double row vector
Here is how you do it
x = [-1 -1 -1 -1 -1 -1]; % check with x = 0; x = -1 etc. as well
if (isscalar(x))
result = x : x - 1
else
result = find(x>0)
end
What Walter Roberson suggested will also work for you.
Walter Roberson
Walter Roberson 2021년 8월 26일
x = [-1]
x = -1
if (isscalar(x))
result = x : x - 1
else
result = find(x>0)
end
result = 1×0 empty double row vector
x = [+1]
x = 1
if (isscalar(x))
result = x : x - 1
else
result = find(x>0)
end
result = 1×0 empty double row vector
x = [-inf]
x = -Inf
if (isscalar(x))
result = x : x - 1
else
result = find(x>0)
end
result = NaN
In that second case, result should not be empty. In the third case, result should be 1x0 not NaN. Your code x:x-1 is presuming an outcome and is using odd operators to try to enforce it.

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

카테고리

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