combine columns with different lengths to create a matrix

조회 수: 32 (최근 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
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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by