i have two Double[] cells with size of 1*n; every element is has some numbers in it . like :
C1{1} = [1 2 3 4]
C2{1} = [5 2 3 7]
.
.
.
C1{n} = [1 2]
C2{n} = [4 5]
how can i have a cell like this :
C{1} = [1 2 3 4 5 7]
.
.
.
C{n} = [1 2 4 5]
i`m totally new to matlab and sorry if its already been answered , but i search a lot and couldn`t find the answer .

댓글 수: 2

Stephen23
Stephen23 2017년 8월 6일
편집: Stephen23 2017년 8월 6일
"two Double[] cells"
A cell array is NOT a double array (but can contain double arrays).
Hamid Salari
Hamid Salari 2017년 8월 6일
@Stephen thanks . i didn`t know that .

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

 채택된 답변

Stephen23
Stephen23 2017년 8월 6일
편집: Stephen23 2017년 8월 6일

1 개 추천

>> C1{1} = [1,2,3,4];
>> C2{1} = [5,2,3,7];
>> C1{2} = [1,2];
>> C2{2} = [4,5];
>> Z = cellfun(@(a,b)unique([a,b]),C1,C2,'uni',0);
>> Z{:}
ans =
1 2 3 4 5 7
ans =
1 2 4 5

추가 답변 (3개)

Jan
Jan 2017년 8월 6일
편집: Jan 2017년 8월 6일

2 개 추천

If you are in doubt, start with a loop:
C1 = {[1 2 3 4], [1 2]};
C2 = {[5 2 3 7], [4 5]};
C = cell(size(C1));
for iC = 1:numel(C)
C{iC} = unique([C1{iC}, C2{iC}]);
end

댓글 수: 3

Stephen23
Stephen23 2017년 8월 6일
편집: Stephen23 2017년 8월 6일
+1 Note that the example has only the unique values (so is not just the concatenation):
C{iC} = unique([C1{iC}, C2{iC}]);
Jan
Jan 2017년 8월 6일
Thanks, Stephen. I hit the Submit button too early.
Hamid Salari
Hamid Salari 2017년 8월 6일
Thanks Jan.

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

Star Strider
Star Strider 2017년 8월 6일
편집: Star Strider 2017년 8월 6일

2 개 추천

This works, and should work with your entire ‘C1’ and ‘C2’:
C1{1} = [1 2 3 4];
C1{2} = [1 2];
C2{1} = [5 2 3 7];
C2{2} = [4 5];
C3 = cellfun(@union, C1, C2, 'Uni',0);
C3{1} % Display Result
C3{2} % Display Result
ans =
1 2 3 4 5 7
ans =
1 2 4 5
EDIT Added output (the ‘ans’ variables).
dpb
dpb 2017년 8월 6일

2 개 추천

That's a little towards the advanced-beginner side... :)
>> C=cellfun(@(c1,c2) unique([c1 c2]),C1,C2,'uniform',0);
>> C{:}
C =
1 2 3 4 5 7
C =
1 2 4 5
>>
The @ symbol is defining an "anonymous function", with two arguments; cellfun passes the content of its two cell array arguments to the defined function cell-by-cell and unique does what it sounds like which is to return the unique values in its argument; the [] simply concatenate the two vectors.

댓글 수: 3

Hamid Salari
Hamid Salari 2017년 8월 6일
Thanks for "anonymous function" . i will look it up . seems handy .
Jan
Jan 2017년 8월 7일
Anonymous functions are very handy, but tend to be slow e.g. in cellfun.
dpb
dpb 2017년 8월 7일
Note Star S's use of UNION over UNIQUE, though...saves a step of the concatenation.
I've never done specific timing; where the syntax is easy I'll write the CELLFUN solution as above and only if it turns out to be a bottleneck worry about what generally would be small differences in run time. Then again, I'm not actively consulting any longer so don't in general ever have large datasets where it would ever be an issue so "caveat emptor" reigneth, methinks. Keep Jan's note in mind if you're still waiting next week for the prompt to come back... :)

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2017년 8월 6일

댓글:

dpb
2017년 8월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by