Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Pre-allocate logical array with unassigned elements (not true or false)

I'm looking for the most efficient method of pre-allocating a logical array in MATLAB without specifying true or false at the time of pre-allocation.

When pre-allocating e.g. a ×ばつ5 numeric array I can use nan(1,5). To my mind, this is better than using zeros(1,5), since I can easily tell which slots have been filled with data versus those that are yet to be filled. If using the zeros() solution it's hard to know whether any 0s are intentional 0s or just unfilled slots in the array.

I'm aware that I can pre-alloate a logical array using true(1,5) or false(1,5). The problem with these is similar to the use of zeros() in the numeric example; there's no way of knowing whether a slot is filled or not.

I know that one solution to this problem is to treat the array as numeric and pre-allocate using nan(1,5), and only converting to a logical array later when all the slots are filled. But this strikes me as inefficient.

Is there some smart way to pre-allocate a logical array in MATLAB and remain agnostic as to the actual content of that array until it is ready to be filled?

Answer*

Draft saved
Draft discarded
Cancel
2
  • 1
    Good, answer Wolfie! But indeed, this is an overkill. If OP has memory issues between single and logical, then they better not accidentally open a tab in chrome because their computer will explode. Commented Jul 21, 2022 at 15:27
  • 1
    @AnderBiguri lol indeed, I saw a challenge and thought I might as well post the results, but I would not use my own class here Commented Jul 21, 2022 at 15:29

lang-matlab

AltStyle によって変換されたページ (->オリジナル) /