combine columns with different lengths to create a matrix

35 vues (au cours des 30 derniers jours)
mehrdad asadi
mehrdad asadi le 7 Juil 2015
Commenté : NMarco le 10 Fév 2021
hi,
how can I create a matrix with vectors with different sizes as columns?
thanks,
  4 commentaires
mehrdad asadi
mehrdad asadi le 7 Juil 2015
for i = 1:sqrt(n)
pci = find(any(cul==i)) %finding # of columns in each patch that contains each column of the image
for j = 1:max(size(pci))
pc(:,:,j) = P_db(:,:,pci(j))
end
end
u see, pci varies in each loop, this code does't work cause of matrix size mismatch. I want to modify 'pci's so that I can create 'pc'!
Ashmil Mohammed
Ashmil Mohammed le 7 Juil 2015
Modifié(e) : Andrei Bobrov le 7 Juil 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

Connectez-vous pour commenter.

Réponse acceptée

Jos (10584)
Jos (10584) le 7 Juil 2015
Many years ago, I wrote the function PADCAT to accomplish this. You can find it on the Matlab File Exchange:
  3 commentaires
Monica Lopez Hidalgo
Monica Lopez Hidalgo le 20 Avr 2020
nice!
NMarco
NMarco le 10 Fév 2021
After 6 years, I found this and, @Jos (10584), you saved my life, my time, my code. Thanks!

Connectez-vous pour commenter.

Plus de réponses (2)

Jan Siegmund
Jan Siegmund le 18 Mai 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{:}]

Guillaume
Guillaume le 7 Juil 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 commentaires
Guillaume
Guillaume le 7 Juil 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
mehrdad asadi
mehrdad asadi le 7 Juil 2015
yeah I got it,
Thanks a lot.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by