How to match matrix dimensions?

11 Ansichten (letzte 30 Tage)
Jackie
Jackie am 23 Jul. 2013
I am making a silly mistake, I am sure, in matching my matrix dimensions. If anyone can spot what I am doing wrong it would be appreciated.
Here is the code to calculate 6 simultaneous equations with 6 unknown variables:
function F = correlation3;
X = [259, 266, 298, 322, 310, 339, 398];
Y = [64.6, 65.48, 65.28, 65.66, 65.86, 65.83, 65.37];
Z = [-42, -35.8, -24.8, -16, -19.7, -9.5 -1];
X2 = X .* X; %Used to simplify the matrix notation within
X3 = X2 .* X; %the while loop.
Y2 = Y .* Y;
Y3 = Y2 .* Y;
Z2 = Z .* Z;
q = [X3 .* X, X3, X2 .* Y2, X2 .* Y, X3 .* Y, X2;
X3, X2, X .* Y2, Y.* X, X2 .* Y, X;
X2 .* Y2, X .* Y2, Y3 .* Y, Y3, Y3 .* X, Y2;
X2 .* Y, X .* Y, Y3, Y2, X .* Y2, Y;
X3 .* Y, X2 .* Y, Y3 .* X, X .* Y2, X2 .* Y2, Y .* X;
X2, X, Y2, Y, Y .* X, 1];
p = [X2.*Z; X.*Z; Y2.*Z; Y.*Z; Y.*Z; Z2];
A, B, C, D, E, F=q\p
end
The error I am presented with is:
"Error using vertcat Dimensions of matrices being concatenated are not consistent.
Error in correlation3 (line 13) q = [X3 .* X, X3, X2 .* Y2, X2 .* Y, X3 .* Y, X2;"
Thanks
  2 Kommentare
Jackie
Jackie am 23 Jul. 2013
Having updated the code with the correction bellow I have a 7 by 42 matrix being produced. Surely this matrix should be a 6 by 1? Seeing as:
"Solution, specified as a vector, full matrix, or sparse matrix. If A is an m-by-n matrix and B is an m-by-p matrix, then x is an n-by-p matrix, including the case when p==1."
And I am starting with a 6 by 6 and a 6 by 1 therefore according to the explanation it should be a 6 by 1?
Youssef  Khmou
Youssef Khmou am 23 Jul. 2013
yes the solution x should be [m,p=1],

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Youssef  Khmou
Youssef Khmou am 23 Jul. 2013
hi Jackie,
The mistake is obvious, its the last component 1, replace it with
ones(1,length(X))
  2 Kommentare
Jackie
Jackie am 23 Jul. 2013
Thank you! Not something I would have found myself but something to remember for the future.
Youssef  Khmou
Youssef Khmou am 23 Jul. 2013
ok, you can still use your first try but with cells
>> doc cell

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by