Wrong with matrix dimensions

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일

0 개 추천

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월 18일
Thanks, it helped a lot. Unfortunately new errors occured instead.
It says now that the vectors has to be the same size. I interpret this as there has to be the same amount of values in the two vectors, which there is.
Matt Tearle
Matt Tearle 2011년 2월 19일
Maybe, but not necessarily. Size in MATLAB means both rows and columns, so a row vector and column vector are different sizes, even if they have the same number of elements.
Can you please copy and post the error message you're getting, so we can see exactly which variables and which line of code it's referring to.
Also, please post the output from the command >> whos This is a useful diagnostic, if you don't know of it. It shows all your variables and their sizes.
Size matching issues are probably about the second most common error messages in MATLAB, so it's good to know how to track them down. Interactively, you can look at the workspace. At the command line, use the whos command.
Lisa
Lisa 2011년 2월 19일
??? Error using ==> polyfit at 48
X and Y vectors must be the same size.
Error in ==> varmepump2 at 24
p1 = polyfit(tidW,acc,3); %anpassar polynom till W(t)
Name Size Bytes Class Attributes
T 120x1 960 double
acc 1x120 960 double
geiger 120x4 3840 double
h 1x1 8 double
i 120x1 960 double
j 120x1 960 double
t1 120x1 960 double
t2 120x1 960 double
temperatur 120x5 4800 double
tidT 120x1 960 double
tidW 120x1 960 double
Matt Tearle
Matt Tearle 2011년 2월 19일
Yep, same issue: acc is a row, tidW is a column. See my other answer for a better way to calculate acc that also keeps it the same size as j (or just transpose it).
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일

0 개 추천

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일

0 개 추천

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!

카테고리

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

태그

질문:

2011년 2월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by