Wrong with matrix dimensions
9 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Matt Tearle
on 18 Feb 2011
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 Comments
Matt Tearle
on 19 Feb 2011
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.
More Answers (2)
Walter Roberson
on 18 Feb 2011
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.
0 Comments
Matt Tearle
on 18 Feb 2011
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!
0 Comments
See Also
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!