How to get rid of the error: Error using horzcat. Dimensions of matrices being concatenated are not consistent.

92 views (last 30 days)
I am trying to change my tutor's matlab code he gave us to suit my own for an assignment. This code is supposed to perform DPCM on an audio signal that I sampled, called "sampleData". This is the error:
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in tryin_a_ting_PCM (line 111) diffpcm = [store, diffpcm];
Below is the relevant segment of my code: % Performing DPCM
SampledData = sampleData(:,1);
store = SampledData(1); diffpcm = SampledData(1:(length(SampledData))-1)-SampledData(2:length(SampledData)); diffpcm = [store, diffpcm];
recon = zeros(1,length(diffpcm)); recon(1) = diffpcm(1); for i = 2:length(recon) recon(i) = recon(i-1) - diffpcm(i); end
% note that 'sampleData' is my sampled audio signal.
In my MATLAB Workspace it says that the variable 'store' has the value '0', and the variable 'diffpcm' has the value '54289x1 double'.
Am I supposed to change the value of 'store' somehow to have a value of '54289x1' to work?

Accepted Answer

Adam
Adam on 25 Sep 2014
Try transposing 'diffpcm'
diffpcm = [store, diffpcm'];
If you want it back as a column vector afterwards then transpose again:
diffpcm = [store, diffpcm']';
54289x1 means 54289 rows and 1 column so you cannot concatenate with a scalar horizontally which has only 1 row. Transposing to a row vector allows you to do so though.

More Answers (2)

Geoff Hayes
Geoff Hayes on 25 Sep 2014
Keegan - you don't have to change the value of store, just change how you are concatenating store with diffpcm.
Note how the latter variable has 54289 rows and one column, whereas the former variable is just a 1x1 scalar - so one row and one column. Horizontal concatenation, like what is being done at diffpcm = [store, diffpcm]; will try to create a matrix with two columns out of the data from store and diffpcm. This means that each of these two variables must have the same number of rows. In this case they don't, and so you observe the error. So you are correct in stating that Am I supposed to change the value of 'store' somehow to have a value of '54289x1' to work? because if they both had the same number of rows, then the horizontal concatenation would succeed.
But seeing as how your code initializes SampledData as a column vector, and that the code later iterates over each element of diffpcm as if it were an array (excluding the first element), then you may want to do a vertical concatenation as
diffpcm = [store; diffpcm];
which will create a 54290x1 array.

Basma
Basma on 25 Jul 2025
Edited: Walter Roberson on 25 Jul 2025
Error using horzcat Dimensions of arrays being concatenated are not consistent.
Error in solution (line 18)
BasisMatrix = [v1.' v2.' I(:,1) I(:,2) I(:,4)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  2 Comments
Image Analyst
Image Analyst on 25 Jul 2025
Not sure how this answers @Keegan's original question. If you have a question of your own, post a question of your own, not here as an answer to someone else's question. And you'd need to tell us the sizes of I, v1, and v2.
If you have any more questions, then create a new question of your own after you read this:
Walter Roberson
Walter Roberson on 25 Jul 2025
I(:,1) and I(:,2) and I(:,4) are both column vectors, of size not specified.
For the error to have occured, one of the following must be true:
  • v1 has a different number of columns than v2
  • v1 has the same number of columns as v2, but the number of colums is different than the number of rows of I
  • exception: it is fine if v1 or v2 have no rows or no columns, but I is not empty; or if v1 and v2 are not empty but I has no rows. 0 x n arrays and n x 0 arrays are both compatible with non-empty arrays when using the [] operator
Note that we are discussing the number of columns of v1 and v2, but the number of rows of I . This is because of the .' transpose operators being applied to v1 and v2.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!