Is it possible to change the matrix resizing setting/preferences?

1 Ansicht (letzte 30 Tage)
Luk
Luk am 29 Jun. 2023
I've been trying to find some info on Matrix resizing settings for a few days now. I will try to describe my problem with a simple example.
1st iteration: The first row of the Matrix is filled with 3 elements.
2nd iteration: The second row of the matrix is filled with 4 elements. => The first row gets a new element "0".
Is it possible to set these resizing preferences using "NaN" elements?
Changes from "0" to "NaN" are not possible in my program because I could have "0" as my real Matrix element.
Thanks
  3 Kommentare
DGM
DGM am 29 Jun. 2023
Avoid growing arrays like that. It's slow, and often leads to unexpected consequences. If you want a specific size, preallocate the output to a specific size. If it's not necessary to create a 2D array of padded variable-length vectors, just use a cell array instead of padding anything.
the cyclist
the cyclist am 29 Jun. 2023
Bearbeitet: the cyclist am 29 Jun. 2023
You are describing a solution to a problem. (Folks here have told you that the solution will not work.)
Instead, can you describe your problem?
It sounds like your problem is how to efficiently fill a matrix with vectors that are of unequal length (and pad the shorter vectors with zeros). Is that right?
Do you know the number of vectors ahead of time? Do you know the length of the longest vector that will be filled in?
Do you need the intermediate arrays along the way, or only the final array?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Arya Chandan Reddy
Arya Chandan Reddy am 30 Jun. 2023
Hi I understand that you are trying to fill the intermediate indices with NaN instead of 0. However there is no direct solution to this, you can't change the preference and this way of filling your matrix is inefficient.
If you still have to achieve this someway then you can try to maintain the indices of real matrix 0s.
Use ismissing to fill 0s with NaNs and replace the NaNs at the indices you've maintained back to 0
Hope it helps

Kategorien

Mehr zu Matrix Indexing 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