Wrong with matrix dimensions

조회 수: 6 (최근 30일)
Lisa
Lisa 2011년 2월 18일
I'm only a beginner when it comes to MatLab and would therefor appreciate some help.
This is my code:
clc
clear
close all
load geiger.txt;
load temperatur.txt; %hämtar filerna med relevant data
i = geiger(:,4);
j = i*4500;
t1 = geiger(:,1);
x = [1:120]; %omvandling till rätt vektor
tidW = (x-1)*10;%omvandling till rätt enhet
t2 = temperatur(:,1);
tidT = (t2-1)*10;
T = temperatur(:,4)+273;
acc(1) = j(1);
for h = 2:length(j) %lägger ihop pulserna från geigermätaren
acc(h)= acc(h-1)+j(h);
end
p1 = polyfit(tidW,acc,3); %anpassar polynom till W(t)
pv1 = polyval(p1,tidW); %gör om värdena med polynomet som grund
pd1 = polyder(pv1); %deriverar polynomet
p = polyval(pd1, tidW)
p2 = polyfit(t2,T,2); %anpassar polynomet T(t)
pv2 = polyval(p2,t2);
pd2 = polyder(p2);
p2 = polyval(pd2,t2);
PH = 10*4190*p2;
e = PH./p;
plot(tidW,acc,'r')
hold on
plot(tidW, pv1,'b')
figure(2)
plot(t2, T,'r')
hold on
plot(t2,pv2,'b')
figure(3)
plot(t2,e)
And MatLab tells me that there's wrong with the matrix dimensions (matrix dimensions must agree)
This is due on tuesday and I'm getting a bit stressed out! All sort of help is highly appreciated!

채택된 답변

Matt Tearle
Matt Tearle 2011년 2월 18일
I assume the error is at the bolded line ( e = PH./p ). It looks like this is due to a row vs column issue (as Walter mentions).
PH comes from p2, which comes from t2, which appears to be a column vector ( t2 = temperatur(:,1); ). So PH should be a column.
p however comes from tidW, which comes from x, which is a row vector ( x = [1:120]; ).
So you're trying to do an element-wise divide of a column by a row. No can do. Try simply transposing x to a column: x = (1:120)';
  댓글 수: 6
Lisa
Lisa 2011년 2월 19일
Okay. Thanks! It is now kind of working. Might be some trouble with a graph, but I'll check my calculations because I think the problem's there!
Once again, thank you!
Matt Tearle
Matt Tearle 2011년 2월 19일
You're very welcome. Can you accept and/or vote for answers that helped, so others know this has been solved (and what the solution was). Thanks. Open a new question if you keep having problems with the plot.

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 2월 18일
Which line is it complaining about, and what are the sizes of the variables involved?
I notice that most of your variables are column vectors. However, because your acc array does not have a preallocated size, it will default to being a row vector. There are certainly cases where the difference between row vector and column vector could cause problems.

Matt Tearle
Matt Tearle 2011년 2월 18일
Aside: you can replace
acc(1) = j(1);
for h = 2:length(j) %lägger ihop pulserna från geigermätaren
acc(h)= acc(h-1)+j(h);
end
with
acc = cumsum(j);
Oh yeah baby!

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by