Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Using horzcat or cat - Easy

Subject: Using horzcat or cat - Easy

From: Kevin Ellis

Date: 6 Mar, 2013 22:49:08

Message: 1 of 2

Hello,

I have an easy problem. My program outputs the following into separate variables, but can be created using these commands (not the issue):

A = [{'Diesel'};{'Gasoline'}];
B = [2;3];

Could someone explain why you cannot horizontally concatenate these two arrays? If I try cat(2,A,B) I get the following:

Error using cat
Dimensions of matrices being concatenated are not consistent.

I get the same using horzcat(A,B). All I want is a matrix that looks like:

C =

'Diesel' 2
'Gasoline' 3

But for some reason I cannot figure out how to do something this simple in MatLab. Any help would be much appreciated.

Kevin

Subject: Using horzcat or cat - Easy

From: dpb

Date: 6 Mar, 2013 23:26:32

Message: 2 of 2

On 3/6/2013 4:49 PM, Kevin Ellis wrote:
> Hello,
>
> I have an easy problem. My program outputs the following into separate
> variables, but can be created using these commands (not the issue):
>
> A = [{'Diesel'};{'Gasoline'}];
> B = [2;3];
>
> Could someone explain why you cannot horizontally concatenate these two
> arrays?

Basically, because B isn't a cell array as written and Matlab can't
figure out what you mean by trying to concatenate the numeric vector
onto the cell array of characters since there's one cell but two
elements in the vector as written...

...

> I get the same using horzcat(A,B). All I want is a matrix that looks like:
>
> C =
> 'Diesel' 2
> 'Gasoline' 3
>
> But for some reason I cannot figure out how to do something this simple
> in MatLab. Any help would be much appreciated.

Well, it'll be difficult to do exactly that but you can at least put the
two into a single cell array if you create B as

B={2;3};

Then

C={A B}

is legal but the display of C by default will look like

 >> A={'A';'B'};
 >> B={2;3};
 >> C={A B}
C =
     {2x1 cell} {2x1 cell}
 >> C{:}
ans =
     'A'
     'B'
ans =
     [2]
     [3]
 >>

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us