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

1 次查看(过去 30 天)
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 个评论
DGM
DGM 2023-6-29
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 2023-6-29
编辑:the cyclist 2023-6-29
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?

请先登录,再进行评论。

回答(1 个)

Arya Chandan Reddy
Arya Chandan Reddy 2023-6-30
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by