Concatenating 2 arrays without changing the dimension

I want to concatenate 2 arrays of same dimension. And I want to add a blank space in between. When I use ' ', the dimension changes to nx2 while I want that the dimension should remain as nx1. Is there any way out to do this? Any help is welcome !!!!
E.g.: 2 arrays are of dimension n x 1. I want to concatenate them with a blank space. And, after that also I want that the dimension should be n x 1 and not n x 2.
Thanks again !!!!

댓글 수: 4

Jan
Jan 2012년 6월 18일
Please give a real example. Are you talking about cell strings?
I'm working on two arrays which include the date and time. So, I wanted to concatenate the two arrays into a single column. Hope that makes clear to you.
Thanks again !!!
Jan
Jan 2012년 6월 18일
Please, Swasti, give an explicite example. E.g.
Input:
C1 = {'26-Jun-2993'; '23-Dec-1998'}
C2 = {'17:23:45', '18:10:17'}
Output:
C3 = {'26-Jun-2993 17:23:45'; '23-Dec-1998 18:10:17'}
???
There are several possible types of "an array containing date and time".
The example is perfect for my case.

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

 채택된 답변

Jan
Jan 2012년 6월 18일
Perhaps you are looking for:
strcat(C1, {' '}, C2)

댓글 수: 4

I tried this but got the error message as
Error using horzcat
CAT arguments dimensions are not consistent.
Hello Jan,
When I try to concatenate them vertically, it works. But when I concatenate them horizontally, it's creating 2 columns. I don't know why !!!! Is there any data overflow or something like that?
strcat(C1(:), {' '}, C2(:))
Thanks a lot Jan !!!!!

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

추가 답변 (1개)

Wayne King
Wayne King 2012년 6월 18일
x = randn(10,1);
y = randn(10,1);
z = [x ;y];
or
z = cat(1,x,y);

댓글 수: 5

Thanks Wayne for the answer. But I've elaborated my question, please have a look. I want to concatenate the arrays horizontally.
I'm not sure I understand. How can you concatenate two arrays of dimension Nx1 into a single column and have the resulting dimension still Nx1? The dimension has to be 2*Nx1 if you don't want them to be Nx2
Sorry for the mistake. Yes, it's 2*Nx1 !!! Thanks a lot !!!
Then why is my answer not correct? What do you mean concatenate horizontally? Do you mean a row vector? Then just transpose the result.
In my case, I have a Nx1 date vector and Nx1 time vector. I want to concatenate the two vectors so that the date and time vector is represented in a single vector (i.e., side by side with a blank space). When I try to use ' ' (blank space), the string concatenates no doubt but there are two columns. I hope you get my query.

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

카테고리

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

질문:

2012년 6월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by