The least square with changing number of lines of matrix

1 次查看(过去 30 天)
Hello. I do some research. I have some measured data - vector, And I have my own linear equations ( - each line of this matrix equation belongs to specific measurement and the point is that sometimes some measurement can not ve taken account) And I have the result- probable vector solved by the least square method. Probable vector is matrix 1x6. Linear equation is 6xn and measured data is 1xn. Minimum of n is 6 and maximum of n is 24. Because I dont know mathlab, I use excel...but some program in mathlab would help me with time. Each time I have to change size of matrixes in excel. And this is pissing me of :-) can you tell me how to do it in mathlab? Actually the first question would be: how many of measurements do you have? Or what is size of vector of measurement data? This changes the size of my linear equation. Maybe I found something like erase the line of matrix. But maybe you found the better way. Thank you for any opinion. Girl from Czechia saying hi😁

回答(2 个)

Star Strider
Star Strider 2023-8-21
I am not certain what you want.
This calculates regressions on each column of ‘y’ and then plots it in the same colours as the original data —
x = (1:6).';
y = randn(6,8);
B = [x, ones(size(x))] \ y
B = 2×8
-0.2606 -0.4523 0.1271 0.0608 0.1484 0.0588 0.3563 0.2241 1.0738 0.4900 -0.9696 -0.3685 -0.4606 0.0353 -0.9595 0.0233
figure
hold on
for k = 1:size(y,2)
hp = plot(x, y(:,k), 'o:');
hp.MarkerFaceColor = hp.Color;
rl = [x, ones(size(x))] * B(:,k);
plot(x, rl, '-', 'Color',hp.Color, 'LineWidth',1)
end
hold off
grid
xlabel('X')
ylabel('Y')
title('Linear Regressions')
B = [x.^2, x, ones(size(x))] \ y
B = 3×8
0.0486 0.3034 0.1011 0.0333 0.0173 0.1494 0.1843 0.1570 -0.6006 -2.5761 -0.5806 -0.1724 0.0271 -0.9871 -0.9338 -0.8746 1.5271 3.3217 -0.0260 -0.0577 -0.2988 1.4299 0.7607 1.4882
figure
hold on
for k = 1:size(y,2)
hp = plot(x, y(:,k), 'o:');
hp.MarkerFaceColor = hp.Color;
rl = [x.^2, x, ones(size(x))] * B(:,k);
plot(x, rl, '-', 'Color',hp.Color, 'LineWidth',1)
end
hold off
grid
xlabel('X')
ylabel('Y')
title('Quadratic Regressions')
B = [x.^3, x.^2, x, ones(size(x))] \ y
B = 4×8
-0.0095 -0.1095 0.0626 0.1919 0.1352 0.0674 -0.0872 0.0085 0.1479 1.4536 -0.5560 -1.9816 -1.4023 -0.5585 1.0998 0.0680 -0.9005 -6.0487 1.4034 5.9108 4.3131 1.1502 -3.6976 -0.6060 1.7655 6.0822 -1.6032 -4.8935 -3.7060 -0.2691 2.9578 1.2746
figure
hold on
for k = 1:size(y,2)
hp = plot(x, y(:,k), 'o:');
hp.MarkerFaceColor = hp.Color;
rl = [x.^3, x.^2, x, ones(size(x))] * B(:,k);
plot(x, rl, '-', 'Color',hp.Color, 'LineWidth',1)
end
hold off
grid
xlabel('X')
ylabel('Y')
title('Third-Order Regressions')
.
  4 个评论
Petlice
Petlice 2023-8-21
Thank you again, Star. No, what I want is really very basic. I have more equations than 6 unknown. And very basically I want only do basic matrix operations (least square) with same linear equations but each time - each turn - each measurement set - has different amount of rows. (completely I have 24 measurements and to this vector 24*1, matrix of 24*6 belongs, so the result is always vector 6*1) and I always have some errors in measurements, so I want to erase some rows from the system. For example, I have wrong, the first, the forth, and the last one measurement, so I want to do for example transponse matrix of A, but the A is of smaller size now. And I dont know how to program the smaller size of matrix....basically, now I am doing this manually. Each time I am choosing which rows will be used and I put them manually to make new matrix of A. Maybe my problem is very stupid. 😁
Star Strider
Star Strider 2023-8-21
Not stupid at all.
I just fail to understand what you want to do from the description.

请先登录,再进行评论。


Bruno Luong
Bruno Luong 2023-8-21
编辑:Bruno Luong 2023-8-21
A = rand(24,6); % your full matrix
b = rand(24,1); % your measurements
% which data you want to remove?
removeidx = [1 4 24]; % removeidx = []; % to remove nothing
keep = setdiff(1:size(b,1), removeidx);
x = A(keep,:) \ b(keep,:)
x = 6×1
0.0582 -0.1722 0.5744 -0.0733 0.2111 0.1379
That's all

产品


版本

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by