Concatenating vectors of different length
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
My code will generate two vectors of different sized in a total of 5 iterations.
Iteration 1- Size- 1x18993
Iteration 2- Size- 1x37986
Iteration 3- Size- 1x75972
Iteration 4- Size- 1x151944
Iteration 5- Size- 1x455832
I want to generate a single matrix of size 5x455832 by padding the previous vectors with NaN.
3 Commenti
Stephen23
il 13 Set 2023
Modificato: Stephen23
il 13 Set 2023
"Hope you understood the problem I faced."
Yes, I do understand the problem you faced, which is why in my previous comment I already told you the solution to that problem.
"For eg after iteration 2, my concatenated array is of size 2x37986, "
Nope, that is not what I told you to do. For some reason you apparently tried to call PADCAT repeatedly inside the loop, attempting to concatenate onto the matrix on each loop iteration. That won't work.
This is what I advised you to do (unlike what you tried, this will work):
C = cell(1,N);
for k = 1:N
V = .. your code that generates vector V
C{k} = V;
end
M = padcat(C{:}); % comma-separated list
Much neater, more robust, and likely more efficient than continuously expanding an array inside a FOR loop.
Risposta accettata
Bruno Luong
il 12 Set 2023
Modificato: Bruno Luong
il 12 Set 2023
result = [];
for k=1:5
veck = randi(10, 1, 2+randi(5))% compute your vector here ..
n = size(veck,2);
result(:,end+1:n) = NaN;
veck(1,n+1:size(result,2)) = NaN;
result(k,:) = veck;
end
result
0 Commenti
Più risposte (1)
NAVNEET NAYAN
il 12 Set 2023
You have to change the size of first 4 vectors obtained in the first 4 iterations according to the size of vector 5.
t1=ones(1,18993);
t2=ones(1,37986);
t3=ones(1,75972);
t4=ones(1,151944);
t5=ones(1,455832);
% Now change size of first 4 vectors as following by appending zeros in the
% end or you can pad NaN in the end
t1 = [t1, zeros(1, length(t5) - length(t1))];
t2 = [t2, zeros(1, length(t5) - length(t2))];
t3 = [t3, zeros(1, length(t5) - length(t3))];
t4 = [t4, zeros(1, length(t5) - length(t4))]; % For NaN, replace zeros from NaN
% Now concatenate these changed vectors to form 5x455832 matrix
Tfinal=cat(1,t1,t2,t3,t4,t5);
0 Commenti
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!