Logical array indexing unexpected behaviour.

조회 수: 2(최근 30일)
Christoffer Bovbjerg
Christoffer Bovbjerg 2022년 7월 28일
편집: Stephen23 2022년 7월 28일
I have a small issue in a program. I have been unable to find anything about this behavior so please enlighten me. I have a simplified example below:
Initialize array:
struct.name = []; struct.value = [];
struct(3).name = 'test';
struct(3).value = 1;
Search struct.name property for string 'test':
strcmp({struct(:).name},'test')
ans =
1×3 logical array
0 0 1
Use logical indexing to get associated value in struct directly:
>> struct(strcmp({struct(:).name}, 'test')).value
ans =
1.0000e+000
Nothing unexpected here, 'test' was found as expected and struct.value for index of 'test' was returned as expected.
Unexpected behavior:
>> strcmp({struct(:).name}, 'not_found')
ans =
1×3 logical array
0 0 0
We see that 'not_found' is not found as reflected by the logical array, no surprises here, but:
>>struct(strcmp({struct(:).name}, 'not_found')).value
>>
If the string is not found, the return is nothing, which i guess makes sense, but this is difficult to manage as the return value would be assigned to a variable in my program. I have not seen anything in MATLAB return nothing, i would expect the empty [] value or similar. In my program i check the return of strcmp to avoid this issue. But I am very curious about this behavior, can someone elaborate?

답변(1개)

Stephen23
Stephen23 2022년 7월 28일
편집: Stephen23 2022년 7월 28일
"I have been unable to find anything about this behavior so please enlighten me"
"I have not seen anything in MATLAB return nothing"
Comma-separated lists can contain zero things.
"i would expect the empty [] value or similar"
No, that would not make sense: if you select zero elements from that structure, then you have generated a comma-separated list which has zero things in it. This consistency makes using comma-separated lists possible:
Note that comma-separated lists do not really "return" anything because they are syntactical constructs, not function calls. Although they look much like a call to SUBSREF et al, I doubt that demand-driven function outputs could actually "return" this functionality (i.e. a list of comma-separated arrays):
"But I am very curious about this behavior, can someone elaborate?"
Dot indexing into a structure generates a comma-separated list. The list contains as many arrays as your dot indexing selects (which in this case is determined by that logical array). If you select zero elements of the structure, then the dot indexing returns a comma-separated list with zero things in it. This is expected, wanted behvaior, and is entirely consistent with how comma-separated lists are defined.
Your proposal would make comma-separated lists more difficult to use, because the zero element-case would need to have special handling in every piece of code, which is most likely to be forgotten and lead to latent bugs everywhere.
If you expect a comma-separated list with one thing in it, then check this e.g. ASSERT NNZ()==1 before the dot indexing.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by