combine each entry of a cell array and a double array
2 次查看(过去 30 天)
显示 更早的评论
I have two arrays - one cell array and one double array, can you please help me how can I combine each element of both arrays.
e.g. A is a cell array of 3 rows (with elements A1,A2,A3) and B is double array of 2 rows (with elements B1 and B2)
I want a 3*2 array like below array
A1B1 A1B2
A2B1 A2B2
A3B1 A3B2
Thanks
0 个评论
采纳的回答
Jos (10584)
2014-2-17
So is A is a cell array of chars, and B is a standard array of doubles, and you want to combine it into a cell array of chars.
A = {'1928.T' '2503.T' '2914.T' '3382.T' '3407.T'}
B = [733263 733264 733267]
[Aix,Bix] = ndgrid(1:numel(A), 1:numel(B))
fh = @(k) sprintf('%s%d',A{Aix(k)},B(Bix(k)))
C = arrayfun(fh,1:numel(Aix),'un',0)
% and reshape, transpose accordingly ...
更多回答(3 个)
Azzi Abdelmalek
2014-2-14
编辑:Azzi Abdelmalek
2014-2-14
A={'A1','A2','A3'}
B={'B1','B2'}
[ii,jj]=ndgrid(1:numel(A),1:numel(B))
strcat(A(ii(:))',B(jj(:))')
%or
A={'A1','A2','A3'}
B={'B1','B2'}
n=numel(A);
m=numel(B);
[ii,jj]=ndgrid(1:n,1:m)
q=reshape(strcat(A(ii(:))',B(jj(:))'),n,m)
% You can add
out=arrayfun(@(x) [q{x,1} ' ' q{x,2}],(1:n)','un',0)
Jos (10584)
2014-2-14
Your talking about doubles in B? What does A1B1 mean in that case? What is A1
% Assuming all doubles
A = {10 ; 11 ; 12}
B = [7 13]
Adouble = [A{:}]
% some juggling with order, transposes and concatenations ...
[bb,aa] = ndgrid(B,Adouble)
R = reshape([aa(:).' ; bb(:).'],numel(B)*2,[]).'
2 个评论
Jos (10584)
2014-2-14
Note that
[aa,bb] = ndgrid(Adouble, B)
R = [aa(:) bb(:))
returns the same information ...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!