
Re: Matrix dimensions must agree.
Posted:
Mar 21, 2014 9:46 AM


"Vitor Andrade" <vitor.luiz.de.andrade@gmail.com> wrote in message news:lghenc$13g$1@newscl01ah.mathworks.com... > "Vitor Andrade" wrote in message > <lgfgag$34p$1@newscl01ah.mathworks.com>... >> dpb <none@non.net> wrote in message <lgfaqk$820$1@speranza.aioe.org>... >> > On 3/20/2014 12:57 PM, Vitor Andrade wrote:
*snip*
> Sr. When I was analyse other data, now, I have a new problem, the matrices > with different sizes; > a = > [0.400000000000000,35139.0781250000;0.933333333333333,32986.5703130000;1.50000000000000,30094.9824220000;2.26905,25000.00000;2.50000000000000,23203.3417970000;3.0666666666666,19190.0585940000;3.60000000000000, > 5137.0966800000;4.16666666666667,10913.9882810000;4.80000000000000,6466.71191400000;5.36666666666667,2564.06420900000;5.86666666666667,377.535583000000] > size(a) > 11x2 > > b = > [0.633333333333333,34091.8632810000;1.23333333333333,31666.7167970000;1.73333333333333,28622.0957030000;2.23333333333333,25087.7421880000;2.76666666666667,21232.4296880000;3.33333333333333,17193.9140625000;3.86666666666667,13042.4101560000;4.46666666666667,8754.32812500000;5.10000000000000,4451.73877000000;5.66666666666667,1339.44116200000] > size(b) > 10x2 > >>>x = length(a); >>>y = length(b);
You shouldn't really use LENGTH on matrices, because you can't be certain that it's telling you what you want to know. [Both a and a' have the same LENGTH, for example, but very different numbers of rows.] But you're interested in the number of rows each of a and b have. Instead, use SIZE to get the size of the matrix _in a specific dimension._
x = size(a, 1); % number of rows in a y = size(b, 1); % number of rows in b
>>> >>> if x > y > a(y+1:end)=[];
This is an indexing technique known as linear indexing as described in the section with that name on this page:
http://www.mathworks.com/help/matlab/math/matrixindexing.html
Instead you want to use subscripted indexing, as described in the "Accessing Multiple Elements" section on that same page.
> else > if y > x > b(x+1:end)=[]; > end > end
I wouldn't use this approach; I'd instead select the elements to keep. It avoids the IF and ELSEIF statements at the expense of a call to MIN:
minRows = min(x, y); a = a(1:minRows, :); b = b(1:minRows, :);
> However, I expected to find: > a = > [0.400000000000000,35139.0781250000;0.933333333333333,32986.5703130000;1.50000000000000,30094.9824220000;2.26905,25000.00000;2.50000000000000,23203.3417970000;3.0666666666666,19190.0585940000;3.60000000000000, > 5137.0966800000;4.16666666666667,10913.9882810000;4.80000000000000,6466.71191400000;5.36666666666667,2564.06420900000]; > > And 'b' were equal. > > But my result was: > > a = > [0.4000,0.9333,1.5000,2.0000,2.5000,3.0667,3.6000,4.1667,4.8000,5.3667];
Yes, because you treated a as though it were a vector.
> You can help me? Please? > > I hope your answer. Thanks so much!
Based on the questions you've been asking, I'm guessing you're fairly new to MATLAB or that it's been a while since you last used it. Is that the case? If so, I recommend you go through the Getting Started documentation. In my opinion it provides a good introduction or refresher on the basics of working with MATLAB, including indexing into arrays. If you open the documentation using the DOC function:
doc
and select MATLAB, you should see something that looks like this page in the documentation browser:
http://www.mathworks.com/help/matlab/index.html
"Getting Started" is right above the Language Fundamentals section.
 Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com

