Storing results from a for loop into a cell array vector

Code so far:
C = {'TGC','TGT'};
D = {'ATA','ATC','ATT'};
k = length(C)*length(D);
V = cell(k,1);
for i=1:length(C)
for j=1:length(D)
V = strcat(C(i),D(j))
end
end
Output:
V =
'TGCATA'
V =
'TGCATC'
V =
'TGCATT'
V =
'TGTATA'
V =
'TGTATC'
V =
'TGTATT'
I just want to store those results in a single vector V. Thanks!

 採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 8 月 16 日
編集済み: Andrei Bobrov 2013 年 8 月 16 日

1 投票

C = {'TGC','TGT'};
D = {'ATA','ATC','ATT'};
[jj,ii] = ndgrid(1:numel(D),1:numel(C));
out = strcat(C(ii(:)),D(jj(:)));
OR
V = cell(length(C)*length(D),1);
k = 1;
for i=1:length(C)
for j=1:length(D)
V(k) = strcat(C(i),D(j));
k = k + 1;
end
end

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2013 年 8 月 15 日

1 投票

V{j} = strcat(C(i),D(j))

1 件のコメント

Jennifer
Jennifer 2013 年 8 月 15 日
It only stores the first 3 results. Is there a way to store all 6?

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

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 15 日

0 投票

nc=numel(C);
nd=numel(D)
ii=repmat(1:nc,1,nd)
jj=repmat(1:nd,nc,1)
out=arrayfun(@(x,y) [C{x} D{y}],ii',jj(:),'un',0)
Jan
Jan 2013 年 8 月 15 日
編集済み: Jan 2013 年 8 月 15 日

0 投票

...
for i=1:length(C)
for j=1:length(D)
V{i, j} = [C{i}, D{j}];
end
end
Now you store all results in the cell V, but what does "in a single vector" mean?

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by