Separating values in cell arrays

Suppose, I have a cell array as follow:
a{1}=[2 3]
I would like to separate the values into two distinct values but at the same time, I want to add a value (in this case ‘1’) to a new column. The output should reflect something like this.
output{1}=[1 2]
output{2}=[1 3]
How can I go about this besides using for loop?

 採用された回答

kei hin
kei hin 2013 年 9 月 4 日

2 投票

Try this
a{1}=[2 3];
[row,col] = size(a{1});
for i = 1:col
for n = 1:row
output{i} = ['1 ',num2str(a{1}(n,i))];
end
end

3 件のコメント

RDG
RDG 2013 年 9 月 4 日
Thanks, but unfortunately, I cannot utilize the output in its char form. Thanks.
kei hin
kei hin 2013 年 9 月 4 日
a{1}=[2 3];
[row,col] = size(a{1});
for i = 1:col
for n = 1:row
output{i} = [1 ,a{1}(n,i)];
end
end
RDG
RDG 2013 年 9 月 4 日
編集済み: RDG 2013 年 9 月 4 日
Simply change the code in order to obtain the desired result,
output{i} = [1,(a{1}(n,i))];

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 9 月 4 日

0 投票

a = arrayfun(@(x)randi(20,1,randi([2 4])),1:5,'un',0); % Let your data
n = cellfun('length',a);
x = zeros(sum(n),2);
x(cumsum(n)-n+1,1) = 1;
x = cumsum(x);
x(:,2) = [a{:}]';
output = num2cell(x,2);

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

質問済み:

RDG
2013 年 9 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by