MATLAB Answers

combine each entry of a cell array and a double array

조회 수: 1(최근 30일)
Mohit
Mohit 14 Feb 2014
댓글: Mohit 17 Feb 2014
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)
Jos (10584) 17 Feb 2014
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 ...

  댓글 수: 1

Mohit
Mohit 17 Feb 2014
This is exactly what I was looking for. Thanks a lot Jos, You are a star!!!!
As you mentioned, I now just need to reshape.

댓글을 달려면 로그인하십시오.

추가 답변(3개)

Thomas
Thomas 14 Feb 2014
편집: Thomas 14 Feb 2014
A= {'A1' 'A2' 'A3'}
B= {'B1' 'B2' }
for ii= 1:length(A)
for jj=1:length(B)
out{ii,jj}=[A{1,ii} B{1,jj}];
end
end
out

  댓글 수: 0

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 14 Feb 2014
편집: Azzi Abdelmalek 14 Feb 2014
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)

  댓글 수: 2

Mohit
Mohit 17 Feb 2014
Array B is double array so it is giving me error.
Mohit
Mohit 17 Feb 2014
Even after converting B to cell array. I am unable to get the desired result. Not sure why this is happening as I dont see elements of double array in the final result. I am beginner in matlab and have gone nuts on this problem. Pls help!
'1928.T' '1928.T' '1928.T'
'2503.T' '2503.T' '2503.T'
'2914.T' '2914.T' '2914.T'
'3382.T' '3382.T' '3382.T'
'3407.T' '3407.T' '3407.T'

댓글을 달려면 로그인하십시오.


Jos (10584)
Jos (10584) 14 Feb 2014
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)
Jos (10584) 14 Feb 2014
Note that
[aa,bb] = ndgrid(Adouble, B)
R = [aa(:) bb(:))
returns the same information ...
Mohit
Mohit 17 Feb 2014
Sorry for not being clear. So lets say A is a cell array with below components
1928.T
2503.T
2914.T
3382.T
3407.T
and B is a double array with below components:
733263
733264
733267
I want a matrix as below:
1928.T733263 2503.T733263 2914.T733263 3382.T733263 3407.T733263
1928.T733264 2503.T733264 2914.T733264 3382.T733264 3407.T733264
1928.T733267 2503.T733267 2914.T733267 3382.T733267 3407.T733267

댓글을 달려면 로그인하십시오.


Translated by