Disagreement of Matrix dimension

Hello Friends,
I have executed the following codes:
X=[8.3 13.9 12.5 22.2 8.3 11.1 18.1 11.1 6.9 6.9 6.9 19.4 9.7 4.2 5.6 12.5 11.1 6.9 8.3 8.3 13.9 9.7 6.9 6.9 8.3 5.6 12.5 4.2 18.1 11.1 4.2 8.3 12.5 15.3 5.6 6.9 13.9 13.9 18.1 12.5 15.3 29.2 36.1 30.6 22.2 40.3 41.7 52.8 50 52.8 61.1 55.6 72.2 69.4 68.1 68.1 76.4 94.4 77.8 101.4 81.9 73.6 93.1 76.4 48.6 52.8 41.7 44.4 43.1 25 26.4 19.4 25 19.4 16.7 13.9 8.3 15.3 5.6 5.6 5.6 5.6 9.7 6.9 2.8 8.3 9.7 8.3 11.1 12.5 15.3 8.3 13.9 4.2 16.7 5.6 8.3 16.7 4.2 11.1 2.8 8.3 5.6 8.3 4.2 11.1 12.5 8.3 8.3 9.7 13.9 15.3 19.4 20.8 25 23.6 25 25 25 33.3 26.4 23.6 27.8 19.4 22.2 19.4 23.6 26.4 15.3 23.6 15.3 26.4 13.9 9.7 15.3 11.1 11.1 18.1 9.7 16.7 18.1 9.7 11.1 22.2 18.1 13.9 19.4 20.8 18.1 13.9 15.3 19.4 13.9 16.7 20.8 12.5 18.1 15.3 15.3 12.5 8.3 12.5 20.8 15.3 15.3 12.5 13.9 9.7 18.1 8.3 19.4 16.7 12.5 12.5 13.9 6.9 9.7 11.1 16.7 5.6 8.3 11.1 4.2 12.5 2.8 12.5 11.1 8.3 9.7 8.3 8.3 18.1 12.5 11.1 8.3 9.7 6.9 12.5 5.6 8.3 0];
M=2;
N = length( X );
Z = ones(N,M) * 1/M; % indicators vector
P = zeros(N,M); % probabilities vector for each sample and each model
t = ones(1,M) * 1/M; % distribution of the gaussian models in the samples
u = linspace(min(X),max(X),M); % mean vector
sig2 = ones(1,M) * var(X) / sqrt(M); % variance vector
C = 1/sqrt(2*pi); % just a constant
Ic = ones(N,1); % - enable a row replication by the * operator
Ir = ones(1,M); % - enable a column replication by the * operator
Q = zeros(N,M);
thresh = 1e-3;
step = N;
last_step = inf;
iter = 0;
min_iter = 10;
when I try to execute the following code,
P = C ./ (Ic*sqrt(sig2)) .* exp( -((X*Ir -Ic*u).^2)./(2*Ic*sig2));
am getting this error:
?? Error using ==> mtimes
Inner matrix dimensions must agree.
The problem I have identified with that code,in particular (X*Ir)is both are matrix but of different sizes, say X = 1R&201C and Ir=1R&1C.
How to handle these different size matrices?
Please help me out.
Also suggest me some alternate codes. Thanks.
kra/-

댓글 수: 2

Oleg Komarov
Oleg Komarov 2011년 8월 4일
Please format the code with the {} button, will increase the chance of an prompt answer.
Anandh
Anandh 2011년 8월 4일
thanks Oleg.

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

 채택된 답변

Andreas Goser
Andreas Goser 2011년 8월 4일

1 개 추천

How to debug this:
1. Format the code in a way Oleg asked for. You help others to help you.
2. If you have a matrix dimension error in one line, split the line into sub-operations in oder to see where it fails, e.g.
P = C ./ (Ic*sqrt(sig2)) .* exp( -((X*Ir -Ic*u).^2)./(2*Ic*sig2)); % fails
exp( -((X*Ir -Ic*u).^2)./(2*Ic*sig2)) % fails
-((X*Ir -Ic*u).^2) % fails
X*Ir % fails - there must be at least one cause
3.
>> whos X Ir
Name Size Bytes Class Attributes
Ir 1x2 16 double
X 1x201 1608 double
-> Now it is clear. 1x201 * 1x2 - > That's not going to work. The question still is, what is it that you need to achieve. Just because I could tell you a working operation, I can't tell you what makes sense.

댓글 수: 3

Anandh
Anandh 2011년 8월 4일
Am working to get the maximum likehood estimate from my sample data. The code I have written there is one among them.Can you tell me a correct operation for that 1x201*1x2 matrix?
Oleg Komarov
Oleg Komarov 2011년 8월 4일
201x1 * 1x2 = 201x2: basics of matrix multiplication, more on wikipedia.
I would suggest to do: (1x2)' * 1x201 = 2x201. Transposing a smaller matrix is more convenient...but it depends also on subsequent operations which at the moment I can't read.
Andreas Goser
Andreas Goser 2011년 8월 4일
I would like to refrain from suggesting a certain operation / implementation.
See the examples in the doc here: http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html
It lists all the ways to multiply two vectors.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by