フィルターのクリア

Turn a cellarray into a single array.

3 ビュー (過去 30 日間)
John Petersen
John Petersen 2015 年 3 月 24 日
コメント済み: John Petersen 2015 年 3 月 24 日
I have a cell array, sig, with varying lengths of data in each cell. For example (on a simplified scale)
sig = [{1}; {[2 3 4]}; {[5 6]}];
n = cellfun(@length,sig);
n is the length of each cell. I want to plot the data in each cell according to their cell index. For this example the answer would be
x = [1, 2,2,2, 3,3]; % indices of each cell
y = [1, 2,3,4, 5,6]; % value of each array element
scatter(x,y,'o');
So the question is, how can I acquire x, y programmatically?

採用された回答

Stephen23
Stephen23 2015 年 3 月 24 日
編集済み: Stephen23 2015 年 3 月 24 日
One solution can be achieved using cellfun and arrayfun. Place this in a script:
sig = {[1], [2,3,4], [5,6]};
N = cellfun('length',sig);
X = arrayfun(@(x,n)x(ones(1,n)), 1:numel(sig),N, 'UniformOutput',false);
X = [X{:}]
Y = [sig{:}]
Running the script displays this in the command window:
>> temp_script
X =
1 2 2 2 3 3
Y =
1 2 3 4 5 6
Note this solution also uses a more efficient way of generating the sig cell array, and a faster cellfun call by using the backwards-compatibility option 'length'.
  1 件のコメント
John Petersen
John Petersen 2015 年 3 月 24 日
Thanks, that works!

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2015 年 3 月 24 日
sig = {[1]; [2 3 4]; [5 6]}; %note that your sig is a cell column
%because sig is a column it has to be transposed in the two instructions below:
X = cell2mat(arrayfun(@(c, p) p*ones(1, numel(c{1})), sig', 1:numel(sig), 'UniformOutput', false))
Y = cell2mat(sig')
  1 件のコメント
John Petersen
John Petersen 2015 年 3 月 24 日
Thanks, this works too!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by