How do I take my generated string outputs from for loop and return a list?
3 次查看(过去 30 天)
显示 更早的评论
seq = 'ACAUUUGCUUCUGACACAACUGUGUUCACUAGCAACCUCAAACAGACA'
for i=1:3:numel(seq)-2
a = (seq(i:i+2))
end
a = 'ACA'
a = 'UUU'
a = 'GCU'
a = 'GAC'
etc...
% Desired output --> ['ACA', 'UUU', 'GCU', 'GAC' ...]
0 个评论
采纳的回答
Walter Roberson
2023-1-19
You should be careful what you ask for. 'ACA' is the 1 x 3 char vector ['A' 'C' 'A'] and ['ACA' 'UUU'] is concatenating two 1 x 3 vectors together getting a 1 x 6 vector 'ACAUUU' -- back to where you started
seq = 'ACAUUUGCUUCUGACACAACUGUGUUCACUAGCAACCUCAAACAGACA'
a1 = [];
a2 = cell(0);
for i=1:3:numel(seq)-2
a1 = [a1, seq(i:i+2)];
a2{end+1} = seq(i:i+2);
end
a1
a2
a3 = reshape(seq, 3, []).'
I would suggest to you that the a3 method is easiest. You might want to cellstr() or string() the resutling 16x3 array.
0 个评论
更多回答(1 个)
Sulaymon Eshkabilov
2023-1-19
Here si the solution:
a=[];
seq = 'ACAUUUGCUUCUGACACAACUGUGUUCACUAGCAACCUCAAACAGACA'
for i=1:3:numel(seq)-2
a = [a; (seq(i:i+2))];
end
a
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!