Combining Data from different Cells in a cell array

조회 수: 5 (최근 30일)
Michelle
Michelle 2013년 7월 30일
So I've been hunting through the answer community, and I have yet to find what I'm looking for. What I'm attempting to do is combine 2 cell arrays so that the cells actually merge together, without using a for loop. What I have are two large cell arrays, one that contains the x coordinates and another that contains the y coordinates. I would like to put them together so that each cell contains both the x and y in two serperate columns.
For example
xcoordinate = {[1;2;3],[3;4;5;6]} %cell array with column vectors in each cell
ycoordinate = {[8;9;10],[12;16;17;10]} %cell array with column vectors in each cell
What I would like the product to be Without using a For loop is:
XYcoordinates = {[1 8;
2 9;
3 10]
[3 12;
4 16;
5 17;
6 10]}
It needs to be in this format with the [x y] sitting next to each other for a function I want to use with cellfun. If you have any suggestions, I would greatly appreciate it. Thanks!
-Michelle
  댓글 수: 1
Jan
Jan 2013년 7월 30일
Why do you want to avoid a FOR loop? Most likely the the cellfun appraoch is slower, so I do not see the benefit.

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

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 7월 30일
cellfun(@(x,y) [x y],xcoordinate',ycoordinate','un',0)
  댓글 수: 1
Michelle
Michelle 2013년 7월 30일
Awesome. I knew it had to be something simple like this, but I completely missed this solution. It does exactly what I wanted it to do. Thanks!

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

추가 답변 (2개)

Jan
Jan 2013년 7월 30일
Why do you want to avoid a FOR loop? Most likely the the cellfun approach is slower, so I do not see the benefit. So compare the speed:
XYCoordinate = cell(size(xcoordinate));
for iC = 1:numel(xcoordinate)
XYCoordinate{iC} = [xcoordinate{iC}, ycoordinate{iC}];
end

Andrei Bobrov
Andrei Bobrov 2013년 7월 30일
strcat(xcoordinate',ycoordinate')

카테고리

Help CenterFile Exchange에서 Data Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by