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:
Issue with horzcat

Subject: Issue with horzcat

From: Jeff

Date: 14 Apr, 2009 03:38:01

Message: 1 of 16

I am trying to concatenate 2 matrices horizontally. I have tried doing this two ways

ortPMat = [ortPMat' newPoint]

and

ortPMat = horzcat(ortPMat',newPoint)

either way I get the error message

??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
 same number of rows.

Error in ==> buildCurve at 45
    ortPmat = horzcat(ortPmat', newPoint);

However, the dimensions of the matrices are 2X51 and 2X1 so they have the same number of rows. I even have the program display the size of each matrix so I can be absolutely sure. What am I missing?

Thanks

Jeff

Subject: Issue with horzcat

From: John Ramshur

Date: 14 Apr, 2009 03:49:02

Message: 2 of 16

ortPMat=[ortPMat, newPoint]

"Jeff " <jhajewsk@indiana.edu> wrote in message <gs10ep$1vr$1@fred.mathworks.com>...
> I am trying to concatenate 2 matrices horizontally. I have tried doing this two ways
>
> ortPMat = [ortPMat' newPoint]
>
> and
>
> ortPMat = horzcat(ortPMat',newPoint)
>
> either way I get the error message
>
> ??? Error using ==> horzcat
> All matrices on a row in the bracketed expression must have the
> same number of rows.
>
> Error in ==> buildCurve at 45
> ortPmat = horzcat(ortPmat', newPoint);
>
> However, the dimensions of the matrices are 2X51 and 2X1 so they have the same number of rows. I even have the program display the size of each matrix so I can be absolutely sure. What am I missing?
>
> Thanks
>
> Jeff

Subject: Issue with horzcat

From: Matt Fig

Date: 14 Apr, 2009 03:52:01

Message: 3 of 16

Since you give the dimensions, I will give it a try:


>> ortPMat = rand(2,51);
>> newPoint = rand(2,1);
>> horzcat(ortPMat,newPoint);
>>


No problem. Why are you taking the transpose?

Subject: Issue with horzcat

From: Jeff

Date: 14 Apr, 2009 15:36:01

Message: 4 of 16

"Matt Fig" <spamanon@yahoo.com> wrote in message <gs1191$q0a$1@fred.mathworks.com>...
> Since you give the dimensions, I will give it a try:
>
>
> >> ortPMat = rand(2,51);
> >> newPoint = rand(2,1);
> >> horzcat(ortPMat,newPoint);
> >>
>
>
> No problem. Why are you taking the transpose?

Well ortPmat is a 51X2 matrix so I have to take the transpose to make it 2X51. I don't know a lot of the in depth details of Matlab but transposing a matrix AFTER I have matlab output its size affects the size that is put out (at least that is what was happening last night). Thanks a lot for your help

Jeff

Subject: Issue with horzcat

From: Matt

Date: 14 Apr, 2009 17:10:04

Message: 5 of 16

"Jeff " <jhajewsk@indiana.edu> wrote in message <gs2ah1$ff4$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <gs1191$q0a$1@fred.mathworks.com>...
> > Since you give the dimensions, I will give it a try:
> >
> >
> > >> ortPMat = rand(2,51);
> > >> newPoint = rand(2,1);
> > >> horzcat(ortPMat,newPoint);
> > >>
> >
> >
> > No problem. Why are you taking the transpose?
>
> Well ortPmat is a 51X2 matrix so I have to take the transpose to make it 2X51. I don't know a lot of the in depth details of Matlab but transposing a matrix AFTER I have matlab output its size affects the size that is put out (at least that is what was happening last night). Thanks a lot for your help
-----------------

At this point, one can only assume that the efforts you made to determine the actual dimensions of the matrices were unsuccessful. Something may have happened to change the matrices between the time when you displayed them and the line where your code crashes.

The thing to do is to type

>>dbstop if error

and then rerun the code. When the code crashes, you will get the K>> prompt at which point you should type

K>>whos ortPMat newPoint

This will display the matrices' dimensions at the actual point in the code where the crash occurs.

Subject: Issue with horzcat

From: Jeff Hajewski

Date: 14 Apr, 2009 17:50:18

Message: 6 of 16

"Matt " <xys@whatever.com> wrote in message <gs2g1c$k4g$1@fred.mathworks.com>...
> "Jeff " <jhajewsk@indiana.edu> wrote in message <gs2ah1$ff4$1@fred.mathworks.com>...
> > "Matt Fig" <spamanon@yahoo.com> wrote in message <gs1191$q0a$1@fred.mathworks.com>...
> > > Since you give the dimensions, I will give it a try:
> > >
> > >
> > > >> ortPMat = rand(2,51);
> > > >> newPoint = rand(2,1);
> > > >> horzcat(ortPMat,newPoint);
> > > >>
> > >
> > >
> > > No problem. Why are you taking the transpose?
> >
> > Well ortPmat is a 51X2 matrix so I have to take the transpose to make it 2X51. I don't know a lot of the in depth details of Matlab but transposing a matrix AFTER I have matlab output its size affects the size that is put out (at least that is what was happening last night). Thanks a lot for your help
> -----------------
>
> At this point, one can only assume that the efforts you made to determine the actual dimensions of the matrices were unsuccessful. Something may have happened to change the matrices between the time when you displayed them and the line where your code crashes.
>
> The thing to do is to type
>
> >>dbstop if error
>
> and then rerun the code. When the code crashes, you will get the K>> prompt at which point you should type
>
> K>>whos ortPMat newPoint
>
> This will display the matrices' dimensions at the actual point in the code where the crash occurs.

I did just as you said and it still says the dimensions are what I thought. Here is what Matlab said

??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
 same number of rows.

Error in ==> buildCurve at 45
    ortPmat = horzcat(ortPmat', newPoint);

45 ortPmat = horzcat(ortPmat', newPoint);
K>> whos ortPmat newPoint
  Name Size Bytes Class

  newPoint 2x1 16 double array
  ortPmat 2x51 816 double array

Grand total is 104 elements using 832 bytes

This error doesn't make sense to me because they have the same number of rows.

Jeff

Subject: Issue with horzcat

From: Matt Fig

Date: 14 Apr, 2009 18:03:40

Message: 7 of 16

"Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message
> I did just as you said and it still says the dimensions are what I thought. Here is what Matlab said
>
> ??? Error using ==> horzcat
> All matrices on a row in the bracketed expression must have the
> same number of rows.
>
> Error in ==> buildCurve at 45
> ortPmat = horzcat(ortPmat', newPoint);
>
> 45 ortPmat = horzcat(ortPmat', newPoint);
> K>> whos ortPmat newPoint
> Name Size Bytes Class
>
> newPoint 2x1 16 double array
> ortPmat 2x51 816 double array
>
> Grand total is 104 elements using 832 bytes
>
> This error doesn't make sense to me because they have the same number of rows.
>
> Jeff



Looks like we are back to post number 3.

Subject: Issue with horzcat

From: Matt

Date: 14 Apr, 2009 18:06:02

Message: 8 of 16

"Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message <gs2icq$b0m$1@fred.mathworks.com>...

> 45 ortPmat = horzcat(ortPmat', newPoint);
> K>> whos ortPmat newPoint
> Name Size Bytes Class
>
> newPoint 2x1 16 double array
> ortPmat 2x51 816 double array
>
> Grand total is 104 elements using 832 bytes
>
> This error doesn't make sense to me because they have the same number of rows.

Yes, it does make sense. In a previous post you told us that ortPmat was 51x2, which necessitated the transpose. The above output contradicts this, however. It says that ortPmat is really 2x51. So, it makes perfect sense that ortPmat' is not compatible with newPoint for concatenation.

Subject: Issue with horzcat

From: Jeff Hajewski

Date: 14 Apr, 2009 18:25:03

Message: 9 of 16

"Matt " <xys@whatever.com> wrote in message <gs2jaa$gv1$1@fred.mathworks.com>...
> "Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message <gs2icq$b0m$1@fred.mathworks.com>...
>
> > 45 ortPmat = horzcat(ortPmat', newPoint);
> > K>> whos ortPmat newPoint
> > Name Size Bytes Class
> >
> > newPoint 2x1 16 double array
> > ortPmat 2x51 816 double array
> >
> > Grand total is 104 elements using 832 bytes
> >
> > This error doesn't make sense to me because they have the same number of rows.
>
> Yes, it does make sense. In a previous post you told us that ortPmat was 51x2, which necessitated the transpose. The above output contradicts this, however. It says that ortPmat is really 2x51. So, it makes perfect sense that ortPmat' is not compatible with newPoint for concatenation.

I took the transpose out and reran the program and here is the output

??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
 same number of rows.

Error in ==> buildCurve at 45
    ortPmat = horzcat(ortPmat, newPoint);

45 ortPmat = horzcat(ortPmat, newPoint);
K>> whos ortPmat newPoint
  Name Size Bytes Class

  newPoint 2x1 16 double array
  ortPmat 52x2 832 double array

Grand total is 106 elements using 848 bytes

The error is in the same line of the code and for the exact same reason. Thanks for your patience. Matt Fig I didn't mean to ignore your post

Jeff

Subject: Issue with horzcat

From: Matt Fig

Date: 14 Apr, 2009 18:30:20

Message: 10 of 16

Is that line the ONLY place you took the transpose off?

Subject: Issue with horzcat

From: Matt

Date: 14 Apr, 2009 18:35:02

Message: 11 of 16

"Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message <gs2kdv$k1j$1@fred.mathworks.com>...
> "Matt " <xys@whatever.com> wrote in message <gs2jaa$gv1$1@fred.mathworks.com>...
> > "Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message <gs2icq$b0m$1@fred.mathworks.com>...
> >
> > > 45 ortPmat = horzcat(ortPmat', newPoint);
> > > K>> whos ortPmat newPoint
> > > Name Size Bytes Class
> > >
> > > newPoint 2x1 16 double array
> > > ortPmat 2x51 816 double array
> > >
> > > Grand total is 104 elements using 832 bytes
> > >
> > > This error doesn't make sense to me because they have the same number of rows.
> >
> > Yes, it does make sense. In a previous post you told us that ortPmat was 51x2, which necessitated the transpose. The above output contradicts this, however. It says that ortPmat is really 2x51. So, it makes perfect sense that ortPmat' is not compatible with newPoint for concatenation.
>
> I took the transpose out and reran the program and here is the output
>
> ??? Error using ==> horzcat
> All matrices on a row in the bracketed expression must have the
> same number of rows.
>
> Error in ==> buildCurve at 45
> ortPmat = horzcat(ortPmat, newPoint);
>
> 45 ortPmat = horzcat(ortPmat, newPoint);
> K>> whos ortPmat newPoint
> Name Size Bytes Class
>
> newPoint 2x1 16 double array
> ortPmat 52x2 832 double array
>
> Grand total is 106 elements using 848 bytes

You made more changes than what you're telling us. If all you did was change line 45 of buildCurve by removing the tranpose, why is ortPmat now of different dimensions than before?

In any case, there is still no mystery as to why the crash occurred. Since ortPmat has 52 rows (now) and newPoint only has 2 rows, it is clear that they cannot be horizontally concatenated.

Subject: Issue with horzcat

From: Jeff Hajewski

Date: 14 Apr, 2009 21:51:01

Message: 12 of 16

"If all you did was change line 45 of buildCurve by removing the tranpose, why is ortPmat now of different dimensions than before?"

That is the whole reason I started this post to begin with-- it does not make any sense.

That transpose is the ONLY change I made. If you want I can post the entire program. I am not making more changes than I am telling you.

Subject: Issue with horzcat

From: Matt

Date: 14 Apr, 2009 22:29:01

Message: 13 of 16

"Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message <gs30g5$dr8$1@fred.mathworks.com>...
> "If all you did was change line 45 of buildCurve by removing the tranpose, why is ortPmat now of different dimensions than before?"
>
> That is the whole reason I started this post to begin with-- it does not make any sense.
-------

The problem you describe now is not the problem you described in your original post. In the original post, you claimed that horzcat was failing to concatenate two matrices which by rights it ought to be able to. However, it is now clear that the two matrices passed to horzcat were never of compatible sizes for this concatenation to be possible.

If there is a problem, horzcat() is not the likely suspect. For whatever reason, the matrices are not the shape you expect by the time they are ready to be passed to horzcat. The change in shape is happening at some previous point in your code.


> That transpose is the ONLY change I made. If you want I can post the entire program. I am not making more changes than I am telling you.
--------

OK. Post 2 versions. The one before the change and the one after.

Subject: Issue with horzcat

From: Jeff Hajewski

Date: 15 Apr, 2009 00:11:02

Message: 14 of 16

Here is the code before taking away the transpose:

function buildCurve(X,Y)
mx = mean(X);
my = mean(Y);
Xtrans = X - mx;
Ytrans = Y - my;

[theta,rho]=cart2pol(Xtrans,Ytrans);

%theta = mod(theta, 2*pi);

%Sort polar coord's so angles are in ascending order.
Pmatrix = [theta rho] %2xN matrix of polar coord's.
Pmatrix2 = Pmatrix.'; %Transpose, giving Nx2 matrix,col1=thetas & col2=rhos.
ortPmat = sortrows(Pmatrix2); %Sorts coord's ascendingly by angle.

%Shift thetas by -2*pi & +2*pi, emphasize periodicity.
NegCopyPmat = ortPmat;
NegCopyPmat(:,1) = NegCopyPmat(:,1) - 2*pi;
PosCopyPmat = ortPmat;
PosCopyPmat(:,1) = PosCopyPmat(:,1) + 2*pi;
%Concatenate transposes of these matrices.
periodPmat = [NegCopyPmat.' ortPmat.' PosCopyPmat.'];
periodPmat = periodPmat.'; %Transpose back.




for j =2:(length(theta)-1)
    thetag(j-1) = -theta(j);
end


theta;
thetag = thetag';
%define

for i=1:length(thetag)
    newRho = spline(periodPmat(:,1),periodPmat(:,2),thetag(i));
    newPoint = [thetag(i)'; newRho];
    %Adds new point to initial Point matrix.
    size(ortPmat)
    size(newPoint)
    ortPmat
    newPoint
    ortPmat = horzcat(ortPmat', newPoint);
    ortPmat = sortrows(ortPmat.');

    %Shifts new ortPmat for periodicity.
    NegCopyPmat = ortPmat;
    NegCopyPmat(:,1) = NegCopyPmat(:,1) - 2*pi;
    PosCopyPmat = ortPmat;
    PosCopyPmat(:,1) = PosCopyPmat(:,1) + 2*pi;

    %Concatenate transposes of these matrices.
    periodPmat = [NegCopyPmat.' ortPmat.' PosCopyPmat.'];
    periodPmat = periodPmat.'; %Transpose back.
  
    
    
end

[newX newY] = pol2cart(THETA,RHO);
newX = newX + mx;
newY = newY + my;
plot(newX,newY)
------------------------------------------------------------------------------------
Matlab Output
-----------------------------------------------------------------------------------

??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
 same number of rows.

Error in ==> buildCurve at 45
    ortPmat = horzcat(ortPmat', newPoint);

45 ortPmat = horzcat(ortPmat', newPoint);
K>> whos ortPmat newPoint
  Name Size Bytes Class

  newPoint 2x1 16 double array
  ortPmat 2x51 816 double array

Grand total is 104 elements using 832 bytes


-------------------------------------------------------------------------------------


Here is the code after taking away the transpose:

function buildCurve(X,Y)

mx = mean(X);
my = mean(Y);
Xtrans = X - mx;
Ytrans = Y - my;

[theta,rho]=cart2pol(Xtrans,Ytrans);

%theta = mod(theta, 2*pi);

%Sort polar coord's so angles are in ascending order.
Pmatrix = [theta rho] %2xN matrix of polar coord's.
Pmatrix2 = Pmatrix.'; %Transpose, giving Nx2 matrix,col1=thetas & col2=rhos.
ortPmat = sortrows(Pmatrix2); %Sorts coord's ascendingly by angle.

%Shift thetas by -2*pi & +2*pi, emphasize periodicity.
NegCopyPmat = ortPmat;
NegCopyPmat(:,1) = NegCopyPmat(:,1) - 2*pi;
PosCopyPmat = ortPmat;
PosCopyPmat(:,1) = PosCopyPmat(:,1) + 2*pi;
%Concatenate transposes of these matrices.
periodPmat = [NegCopyPmat.' ortPmat.' PosCopyPmat.'];
periodPmat = periodPmat.'; %Transpose back.




for j =2:(length(theta)-1)
    thetag(j-1) = -theta(j);
end


theta;
thetag = thetag';
%define

for i=1:length(thetag)
    newRho = spline(periodPmat(:,1),periodPmat(:,2),thetag(i));
    newPoint = [thetag(i)'; newRho];
    %Adds new point to initial Point matrix.
    size(ortPmat)
    size(newPoint)
    ortPmat
    newPoint
    ortPmat = horzcat(ortPmat, newPoint);
    ortPmat = sortrows(ortPmat.');

    %Shifts new ortPmat for periodicity.
    NegCopyPmat = ortPmat;
    NegCopyPmat(:,1) = NegCopyPmat(:,1) - 2*pi;
    PosCopyPmat = ortPmat;
    PosCopyPmat(:,1) = PosCopyPmat(:,1) + 2*pi;

    %Concatenate transposes of these matrices.
    periodPmat = [NegCopyPmat.' ortPmat.' PosCopyPmat.'];
    periodPmat = periodPmat.'; %Transpose back.
  
    
    
end

[newX newY] = pol2cart(THETA,RHO);
newX = newX + mx;
newY = newY + my;
plot(newX,newY)
--------------------------------------------------------------------------------------------------------------
Matlab Output
--------------------------------------------------------------------------------------------------------------
??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
 same number of rows.

Error in ==> buildCurve at 45
    ortPmat = horzcat(ortPmat, newPoint);

45 ortPmat = horzcat(ortPmat, newPoint);
K>> whos ortPmat newPoint
  Name Size Bytes Class

  newPoint 2x1 16 double array
  ortPmat 52x2 832 double array

Grand total is 106 elements using 848 bytes

Subject: Issue with horzcat

From: Jiro Doke

Date: 15 Apr, 2009 01:52:01

Message: 15 of 16

"Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message <gs38mm$4pv$1@fred.mathworks.com>...
>
> Here is the code after taking away the transpose:
>

*<snip>*

> ortPmat = horzcat(ortPmat, newPoint);
> ortPmat = sortrows(ortPmat.');
>

Okay, I think the problem happens here (and the fact that this runs in a loop, recursively). After you do "horzcat", you transpose it to do "sortrows" (I assume you want to sort by columns). You should transpose it back:

ortPmat = sortrows(ortPmat.').';


Then, of course, you may have to fix the rest of the code below. *This may not be completely accurate, but here goes*

NegCopyPmat = ortPmat;
NegCopyPmat(1,:) = NegCopyPmat(1,:) - 2*pi;
PosCopyPmat = ortPmat;
PosCopyPmat(1,:) = PosCopyPmat(1,:) + 2*pi;

%Concatenate transposes of these matrices.
periodPmat = [NegCopyPmat ortPmat PosCopyPmat];
periodPmat = periodPmat; %Transpose back.



-----
jiro

Subject: Issue with horzcat

From: Jeff Hajewski

Date: 16 Apr, 2009 00:39:02

Message: 16 of 16

Very nice. Thanks a lot- that solved the problem

Jeff

"Jiro Doke" <jiro.doke@mathworks.com> wrote in message <gs3ek1$7sk$1@fred.mathworks.com>...
> "Jeff Hajewski" <jhajewsk@indiana.edu> wrote in message <gs38mm$4pv$1@fred.mathworks.com>...
> >
> > Here is the code after taking away the transpose:
> >
>
> *<snip>*
>
> > ortPmat = horzcat(ortPmat, newPoint);
> > ortPmat = sortrows(ortPmat.');
> >
>
> Okay, I think the problem happens here (and the fact that this runs in a loop, recursively). After you do "horzcat", you transpose it to do "sortrows" (I assume you want to sort by columns). You should transpose it back:
>
> ortPmat = sortrows(ortPmat.').';
>
>
> Then, of course, you may have to fix the rest of the code below. *This may not be completely accurate, but here goes*
>
> NegCopyPmat = ortPmat;
> NegCopyPmat(1,:) = NegCopyPmat(1,:) - 2*pi;
> PosCopyPmat = ortPmat;
> PosCopyPmat(1,:) = PosCopyPmat(1,:) + 2*pi;
>
> %Concatenate transposes of these matrices.
> periodPmat = [NegCopyPmat ortPmat PosCopyPmat];
> periodPmat = periodPmat; %Transpose back.
>
>
>
> -----
> jiro

Tags for this Thread

No tags are associated with 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