I have two arrays, can you please help me how can I concatenate each element of both arrays.
e.g. A=['a' 'b' 'c'] B = ['1' '2']
I want ['a1' 'a2' 'b1' 'b2' 'c1' 'c2']
Thanks

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 1일
편집: Azzi Abdelmalek 2014년 2월 1일

3 개 추천

A=['a' 'b' 'c'];
B = ['1' '2'];
[ii,jj]=ndgrid(1:numel(A),1:numel(B));
out=arrayfun(@(x,y) [A(y) B(x)],jj(:),ii(:),'un',0);
out=sort(out)

댓글 수: 3

Wayne King
Wayne King 2014년 2월 1일
+1 that's neat Azzi
Mohit
Mohit 2014년 2월 3일
Thanks Azzi!
No need for arrayfun to concatenate, or using sort when you re-arrange the inputs and outputs to/from ndgrid:
[bb,aa] = ndgrid(B,A) ;
C = [aa(:) bb(:)]

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

추가 답변 (3개)

Jan
Jan 2014년 2월 1일

1 개 추천

A = {'a' 'b' 'c'};
B = {'1' '2'};
C = strcat(repmat(A, 2, 1), repmat(B', 1, 3))
Notice that A and B are cell strings, not char vectors.
Wayne King
Wayne King 2014년 2월 1일

0 개 추천

Make A and B cell arrays of strings, but B has to be the same length as A so you'll have to repeat the 2
A = {'a','b','c'};
B = {'1','2','2'};
strcat(A,B)

댓글 수: 1

Mohit
Mohit 2014년 2월 3일
Thanks, it will give me 3 elements in result matrix while I wanted 6.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2014년 2월 1일

댓글:

2014년 2월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by