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

522 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 (1)

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.

Categories

Find more on Linear Algebra 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!