combine columns with different lengths to create a matrix
35 views (last 30 days)
Show older comments
hi,
how can I create a matrix with vectors with different sizes as columns?
thanks,
4 Comments
Ashmil Mohammed
on 7 Jul 2015
Edited: Andrei Bobrov
on 7 Jul 2015
a=rand(10,1); % randomly make two vectors replace this with your code
b=rand(8,1);
s=size(b);z=size(a);% (find size of both)
d=[b;zeros(z(1)-s(1),1)]; % concatenate the smaller with zeros
c=[a,d]; % catenate the two vectors
Accepted Answer
Jos (10584)
on 7 Jul 2015
Many years ago, I wrote the function PADCAT to accomplish this. You can find it on the Matlab File Exchange:
More Answers (2)
Jan Siegmund
on 18 May 2020
If the input data is a cell array of column vectors, you might consider this:
a = {ones(5,1) ones(8,1)}; %test data
len = max(cellfun('length',a));
b = cellfun(@(x)[x;zeros(len-size(x,1),1)],a,'UniformOutput',false);
out = [b{:}]
0 Comments
Guillaume
on 7 Jul 2015
First, some comments on the code you've posted:
for i = 1:sqrt(n)
pci = find(any(cul==i))
for j = 1:max(size(pci))
pc(:,:,j) = P_db(:,:,pci(j))
end
end
You're using find to convert a logical array (returned by any) into an array of indices, and then using these indices to index into a matrix. The find is unnecessary work. Just use the logical indices directly.
Secondly max(size(v)) to find the numbers of elements in a vector is a very strange construct. numel(v) is the function you should be using.
Third, you don't need a loop to copy the elements. As a result, the code you've posted could be rewritten as:
for i = 1:sqrt(n)
pci = any(cul == i); %pci a logical array
pc = P_db(:, :, pci);
end
I'll also note that in the above code and in your example code the result pc is overwritten in each loop, so it does not matter if it changes size. I assume you meant to store it in something else. Finally, you mention concatenating column vectors, but according to your code pc is a 3D matrix.
Now, to answer your question, the best thing is to store pc in a cell array. Then it does not matter if it changes size on each iteration:
for i = 1:sqrt(n)
pci = any(cul == i); %pci a logical array
pc{i} = P_db(:, :, pci); %store in a cell array
end
Afterward, you could pad all these 3d matrices to the maximum size and concatenate them all as a 4D matrix, but I'm not sure why you'd want to:
allpcsizes = cellfun(@size, pci, 'UniformOutput', false);
max3dsize = max(vertcat(allpcsizes{:}));
for i = 1:numel(pc)
resizedpc = zeros(max3dsize);
resizedpc(1:size(pc{i}, 1), 1:size(pc{i}, 2), 1:size(pc{i}, 3)) = pc{i};
pc{i} = resizedpc;
end
pc4d = cat(4, pc{:});
5 Comments
Guillaume
on 7 Jul 2015
@mehrdad: . I used 'find' because I need the column indices with contain 'i'. without 'find' I get a vector with elements of 0 or 1 which I should again check for the indie of column
You don't need to use find, you can pass the vector of 0 and 1, a logical array as indices, matlab will only keep the elements for which the logical array is 1.
To access your '2nd matrix in a cell' (the wording is a bit sloppy, it's the 2nd page of the single matrix in the cell you want:
ma= pd{i}(:, :, 2); %2nd matrix in cell i
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!