combine columns with different lengths to create a matrix

48 ビュー (過去 30 日間)
mehrdad asadi
mehrdad asadi 2015 年 7 月 7 日
コメント済み: NMarco 2021 年 2 月 10 日
hi,
how can I create a matrix with vectors with different sizes as columns?
thanks,
  4 件のコメント
mehrdad asadi
mehrdad asadi 2015 年 7 月 7 日
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 2015 年 7 月 7 日
編集済み: Andrei Bobrov 2015 年 7 月 7 日
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

サインインしてコメントする。

採用された回答

Jos (10584)
Jos (10584) 2015 年 7 月 7 日
Many years ago, I wrote the function PADCAT to accomplish this. You can find it on the Matlab File Exchange:
  3 件のコメント
Monica Lopez Hidalgo
Monica Lopez Hidalgo 2020 年 4 月 20 日
nice!
NMarco
NMarco 2021 年 2 月 10 日
After 6 years, I found this and, @Jos (10584), you saved my life, my time, my code. Thanks!

サインインしてコメントする。

その他の回答 (2 件)

Jan Siegmund
Jan Siegmund 2020 年 5 月 18 日
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 2015 年 7 月 7 日
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 件のコメント
Guillaume
Guillaume 2015 年 7 月 7 日
@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 2015 年 7 月 7 日
yeah I got it,
Thanks a lot.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by