deal array contents into cell arrays

조회 수: 4 (최근 30일)
Gabriel Stanley
Gabriel Stanley 2022년 12월 9일
편집: Stephen23 2022년 12월 10일
I'm trying to figure out how to assign the values of an array to targeted cell arrays. E.g. I have a 16x6 cell array, and I want to distribute the 32 values of a 16x2 string array into the first two column of the cell, i.e CellArray{1,1} = StringArray(1,1), CellArray{16,2} = String(16,2), etc.
Given that I cannot know the size of the arrays a priori, do I need to figure out an annonymous function using deal to convert the MxN string array into M*N string scalars?
  댓글 수: 1
Szemis
Szemis 2022년 12월 9일
The task is much easier if you can convert your string array into a cell array.
% a simple example
m = 16;
n = 4;
o = 2;
CellArray = cell(m,n);
String = strings(m,o);
cArr = num2cell(String);
[CellArray{1:m*o}] = cArr{:};

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

채택된 답변

Stephen23
Stephen23 2022년 12월 9일
편집: Stephen23 2022년 12월 9일
C = cell(3,2)
C = 3×2 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
S = ["cat","hat";"hello","world";"A","B"]
S = 3×2 string array
"cat" "hat" "hello" "world" "A" "B"
[C{:}] = deal(S{:})
C = 3×2 cell array
{'cat' } {'hat' } {'hello'} {'world'} {'A' } {'B' }
  댓글 수: 2
Gabriel Stanley
Gabriel Stanley 2022년 12월 9일
Thank you. WIthout a link to the tutorial, which didn't not come up in my google searching, I would not have realized string arrays were also eligible for {} indexing. While the example I gave used string arrays, I am interested to know how to do this with other data types. I've tried to replicate the assignment with integer values, but to no avail. I cannot tell from either of the linked articles how to go about performing this kind of assignment in the generic case.
Stephen23
Stephen23 2022년 12월 10일
편집: Stephen23 2022년 12월 10일
"I cannot tell from either of the linked articles how to go about performing this kind of assignment in the generic case."
As my tutorial clearly states, comma-separated lists can be constructed from three particular data types. There is no "generic case" that applies to all data types.
Of course there is nothing stopping you from creating a cell array of data and using normal parenthesis indexing to assign it to the LHS cell array:
LHScellarray(:) = RHScellarray
This is completely unrelated to comma-separated lists.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by