if we have multiple vectors, A,B,C. Each of them has different length. How can we compensate them by NaN into same length and store them as a matrix.

 Akzeptierte Antwort

Stephen23
Stephen23 am 31 Dez. 2022

0 Stimmen

The simplest approach is to download the function PADCAT() here:
and use it like this:
M = padcat(A,B,C)

3 Kommentare

DDDD
DDDD am 31 Dez. 2022
Thank you for your answer! Really appreciate!
Indeed, I will receive a vector from a function, and I want to compensate it into a certian length. For example, I have a loop for 4 times, within each loop I will get a vector, then I need to compensate the vector with NaN within that loop; then in next loop I will have new vector to compensate.
Or to say, I can NOT have A, B , C ,D vector at one time, and I need to compensate all of them....
maybe I can set a vector ' S' with all NaN and enough length, then within each loop, I compensate A/B/C/D with S.
Stephen23
Stephen23 am 31 Dez. 2022
Bearbeitet: Stephen23 am 31 Dez. 2022
"For example, I have a loop for 4 times, within each loop I will get a vector, then I need to compensate the vector with NaN within that loop; then in next loop I will have new vector to compensate."
How will you know what length to pad to, if you do not have all of the vectors?
I suspect that you could simply store all of the vectors in a cell array, and then supply them to PADCAT after the loop. This would likely be the simplest approach:
N = 4;
C = cell(1,N); % preallocate!
for k = 1:N
V = your code that generates a vector;
C{k} = V;
end
M = padcat(C{:})
DDDD
DDDD am 3 Jan. 2023
Yes you are right and your solution is the straightforward and simple!!! Thanks Stephen, it really helps me!!!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

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

Produkte

Version

R2022b

Tags

Gefragt:

am 31 Dez. 2022

Kommentiert:

am 3 Jan. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by