how to convert array of cells within array of cells in single cell array

조회 수: 3 (최근 30일)
Mausmi Verma
Mausmi Verma 2021년 12월 26일
편집: Image Analyst 2021년 12월 26일
A= {<1x1 cell> <1x3 cell> [4,0] <1x4 cell>}
where
<1x1>= [2,3,4]
<1x3>= [ ] [3,4] [3,8,13]
<1x4>= [9,4] [9,8,13] [ ] [9,14,19]
so i want my answer to be in form of a single array of cells as below,
B={ [2,3,4] [3,4] [3,8,13] [4] [9,4] [9,8,13] [9,14,19] }
  댓글 수: 1
Image Analyst
Image Analyst 2021년 12월 26일
편집: Image Analyst 2021년 12월 26일
OK I see that you want null cells to be left out, but how did cell number 4 come to contain [4]:
B={ [2,3,4] [3,4] [3,8,13] [4] [9,4] [9,8,13] [9,14,19] }
The third cell of A contains a vector [4, 0]. But B{4} is [4]. What happened to the 0? Do you want to exclude zeros also for some reason?

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

답변 (1개)

Stephen23
Stephen23 2021년 12월 26일
편집: Stephen23 2021년 12월 26일
A = {{[2,3,4]},{ [],[3,4],[3,8,13]},{[4,0]},{[9,4],[9,8,13],[],[9,14,19]}}
A = 1×4 cell array
{1×1 cell} {1×3 cell} {1×1 cell} {1×4 cell}
B = [A{:}]
B = 1×9 cell array
{[2 3 4]} {0×0 double} {[3 4]} {[3 8 13]} {[4 0]} {[9 4]} {[9 8 13]} {0×0 double} {[9 14 19]}
  댓글 수: 3
Voss
Voss 2021년 12월 26일
@Stephen's answer creates a single cell array, as requested. The only difference is between his result and the result you want is that his result retains the empty elements from the original cell array. You can remove the empty elements like this:
B(cellfun(@(x)isempty(x),B)) = [];
Stephen23
Stephen23 2021년 12월 26일
편집: Stephen23 2021년 12월 26일
Or without the anonymous function:
B(cellfun(@isempty,B)) = [];
"...so i want my answer to be in single cell array"
and is what my answer gives you.

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

카테고리

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