Computing a numerical value but gets output of 2 columns array?
2 次查看(过去 30 天)
显示 更早的评论
Hi. I'm new to Matlab coming from python. My prof requires us to use Matlab as the main tool for the course. I'm currently making an algorithm to compute the least square value of the given data.
I'm currently getting a problem where I compute a numerical value but somehow the output shows me a 2 columns array. In this code, I'm talking about the variable a1a, a1b, a0, avgX and avgY. I understand why a0 gives me an array because the input (avgX and avgY) are an array. But, on the other hand, the inputs for a1a, a1b, avgX and avgY are integers/numerical values (based on the workspace shows), so why on the earth they would somehow become a 2 column array?
What did I do wrong? Can someone point me? I know my Matlab knowledge is quite basic, but this abstract problem frustrates me much!
In case of someone wonders, I previously use a '/' sign (without a dot sign on the front) for avgX and avgY equation, but it always gave me an error so I decided to replace it with a './' sign.
arrX = [0 2 4 6 9 11 12 15 17 19];
arrY = [5 6 7 6 9 8 7 10 12 12];
n = size(arrX);
sumX = 0;
sumY = 0;
sumXY = 0;
sumX2 = 0;
for iter = 1 : 10;
sumX = sumX + arrX(iter);
sumY = sumY + arrY(iter);
sumXY = sumXY + (arrX(iter)*arrY(iter));
sumX2 = sumX2 + (arrX(iter)^2);
end;
sum2X = sumX^2;
avgX = sumX./n;
avgY = sumY./n;
a1a = (n*sumXY)-(sumX*sumY);
a1b = (n*sumX2)-sum2X;
a1 = a1a/a1b;
a0 = avgY-(a1*avgX);
1 个评论
采纳的回答
KSSV
2018-4-17
编辑:KSSV
2018-4-17
n = size(arrX) ;
This is not correct.....use:
n = size(arrX,2) ; or n = length(arrX) ;
Note that, you can use in built functions....to get what you want. Read about sum, mean etc,.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!