Wrong with matrix dimensions

6 次查看(过去 30 天)
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