How to concatenate cells?

조회 수: 10 (최근 30일)
Fabricio
Fabricio 2013년 3월 12일
Hello,
I have 2 cell matrices like <1*1150cell> one has 1000*2 matrix in each cell: xxp the other has 1000*4 matrix in each cell: xxq
I would like to have a one cell matrices of <1*1150cell> with each cell having 1000*6 matrix from the concatenating both matrices.
I have tried different approaches, and tried resorting with loops but was not able to do it.
for i=1:1150
xx{i}={[xxp{i} xxq{i}]};
end
This does not work there are other longer ways to go apparently. But provides an idea of the problem.

답변 (3개)

Honglei Chen
Honglei Chen 2013년 3월 12일
I think you just need to do
for i=1:1150
xx{i}=[xxp{i}; xxq{i}];
end
Alternatively, you can do
xx = cellfun(@(x,y)[x;y],xxp,xxq,'UniformOutput',false);
  댓글 수: 1
Fabricio
Fabricio 2013년 3월 12일
It seems not to work both ways, but thanks for the answer.

댓글을 달려면 로그인하십시오.


Konrad Malkowski
Konrad Malkowski 2013년 3월 12일
Does this do what you are looking for?
A = repmat([1,2,3,4], 1000, 1);
B = repmat([5,6], 1000, 1);
C = {A};
D = {B};
CC = repmat(C, 1, 1115);
DD = repmat(D, 1, 1115);
XX = cellfun(@horzcat, CC, DD, 'UniformOutput',false)
  댓글 수: 1
Fabricio
Fabricio 2013년 3월 12일
Thank you, indeed, it does the trick. Nice way to go

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 3월 12일
One way would be
mat2cell( [cell2mat(xxp.'), cell2mat(xxq.')], ones(1,1150), 6) .'
But this uses for loops internally.
  댓글 수: 1
Fabricio
Fabricio 2013년 3월 12일
It seems that it could work, but the .' at the end is not accepted It seems quite simple and straight forward though. Thanks anyways for your input

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by