How to match matrix dimensions?

조회 수: 10 (최근 30일)
Jackie
Jackie 2013년 7월 23일
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
Jackie
Jackie 2013년 7월 23일
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 2013년 7월 23일
yes the solution x should be [m,p=1],

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by