Filter löschen
Filter löschen

Pre allocate memory for unknown variable size

3 Ansichten (letzte 30 Tage)
Ricky
Ricky am 22 Mai 2013
Hello Everyone,
I have a cell array whose dimensions are 1000*1 . These are sensor scans from a laser and always I get a cell array with above dimensions. When the sensor is ON the data gets stored in the above cell array.
So for example in trial 1 laser sensor is ON for 5 seconds then the cell array has data in only first 180 cells,the rest of the cells are empty. In the trial 2 laser sensor maybe ON for 6 seconds and I can get data in 200 cells.
Currently I am using a for loop to iterate through the cell array and check and store data for non-zero elements. But this reduces speed of my code as I am not able to pre allocate memory.
Can anyone suggest an alternative method.
Regards
  1 Kommentar
Jan
Jan am 22 Mai 2013
Please post the code you use. Otherwise it is hard to suggest improvements. Perhaps a cellfun('isempty', C) is sufficient already, or an extra list of occupied indices.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Iain
Iain am 22 Mai 2013
Preallocate to the largest size you will ever need, and for the unused elements, you can set them to a known bad value. eg. NaN, Inf, -Inf, or clear those elements with:
Returned(57:end) = [];

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by