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

조회 수: 2 (최근 30일)
Luk
Luk 2023년 6월 29일
답변: Arya Chandan Reddy 2023년 6월 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