matlab horzcat CAT arguments dimensions are not consistent.

1 view (last 30 days)
im using a matrix DATA with dimensions<245x2 double>
then
SECOND = DATA(:,2) >= 8 & DATA(:,2) <= 65 FIRST = DATA(:,1) & SECOND
When i try DATA(FIRST,1) and DATA(SECOND,2) they are different dimensions 72x1 and 73x1
how can i make them the same dimension so i can merge them in
TDOT = [DATA(FIRST,1) DATA(SECOND,2)]

Accepted Answer

Star Strider
Star Strider on 29 Aug 2014
Edited: Star Strider on 29 Aug 2014
One option is that if they are not integer arrays, you could use the interp1 function to lengthen the shorter vector using the 'extrap' option. It depends what your data are, and what you are comfortable with. Essentially, interpolation-extrapolation amounts to ‘stretching’ the shorter vector a bit, keeping all the essential information.
Another option is that SECOND for some reason has duplicate values (that it is not supposed to) and that is causing the disparity. (Guessing here.) You could use the unique function to find the duplicated value in the longer vector and eliminate it.

More Answers (1)

Geoff Hayes
Geoff Hayes on 29 Aug 2014
Saúl - if you want to merge the two matrices as shown above (horizontally), the two matrices must have the same number of rows. So either remove one row from the second matrix (since it has 73 rows) or just add a row of zeros to the first matrix
TDOT = [ [DATA(FIRST,1);0] DATA(SECOND,2) ]
Try the above and see what happens!
  2 Comments
Saúl
Saúl on 29 Aug 2014
Thanks for your answer Geoff.
The problem is that SECOND is suppose to take the values from DATA that match my criteria ( in this case, any number that is >=8 and <=65) and FIRST is supposed to take the only the values that match those from SECOND.
So, in theory, FIRST and SECOND should always be the same length.
This is not the case, however, as FIRST always ends up being 1 row smaller.
I'm sorry if i'm not clear enough, but im not sure what else i should mention.
Geoff Hayes
Geoff Hayes on 29 Aug 2014
My mistake, I hand't read the question closely enough. I'm not sure why the above code would set FIRST to one element less than SECOND. I suppose that you could figure out which row is being removed from FIRST by comparing it with SECOND. They both should have ones and zeros in the same place, except for one index where FIRST would have a zero and SECOND a one.
But do you really need both a FIRST and SECOND, since FIRST is supposed to take the only the values that match those from SECOND.
Your code could simplify to
TDOT = DATA(SECOND,:);
which would mean grab data from both columns where SECOND indicates which rows of the second column meet the specified criteria.

Sign in to comment.

Categories

Find more on Sparse Matrices 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!