Loss is still larger than 30?
1 次查看(过去 30 天)
显示 更早的评论
Loss is still larger than 30?
i need these columns a(:,2:2:24) to be close to these ones a(:,1:2:24) and a(:,2:2:24) should be smaller than a(:,1:2:24)
Loss = zeros(27, 12); % Adjust the size according to the number of pairs
for ii = 1:27
colIndex = 1; % Initialize column index for Loss matrix
for i = 1:2:24
% Calculate initial loss
Loss(ii, colIndex) = (a(ii, i) - a(ii, i+1)) * 100 / a(ii, i);
% Check condition and adjust if necessary
if Loss(ii, colIndex) < 0 && a(ii, i) < a(ii, i+1)
a(ii, i) = a(ii, i+1) + 0.00004;
elseif Loss(ii, colIndex) > 30
a(ii, i+1) = a(ii, i) - 0.00004;
end
% Recalculate loss after adjustment
Loss(ii, colIndex) = (a(ii, i) - a(ii, i+1)) * 100 / a(ii, i);
colIndex = colIndex + 1; % Increment column index for Loss matrix
end
end
% Display the updated matrix 'a'
disp(a);
% Display the Loss matrix
disp(Loss);
2 个评论
John D'Errico
2024-5-24
编辑:John D'Errico
2024-5-24
How can we possibly help you, if you do not supply the matrix a? Even then, if we are given no clue what this code is supposed to do, it is just random code. Since it does not do what you seem to expect it to do, then it might not even be correct code.
I've tried the tea reading toolbox, to see if it would tell me what a is, but it was of no value. Admittedly, I may have used the wrong kind of tea. And since I dropped my crystal ball, it now has a large crack in it. The new one is on order, but you know how mail order works these days.
采纳的回答
Steven Lord
2024-5-24
Let's take a look at a few lines of your code. I've commented them out since I want to also run some code and these code snippets would not execute.
%{
elseif Loss(ii, colIndex) > 30
a(ii, i+1) = a(ii, i) - 0.00004;
end
%}
So if this else block executes we know that a(ii, i+1) - a(ii, i) is effectively -0.00004. So what does that mean for your recalculation?
%{
% Recalculate loss after adjustment
Loss(ii, colIndex) = (a(ii, i) - a(ii, i+1)) * 100 / a(ii, i);
%}
a(ii, i) - a(ii, i+1) is effectively 0.00004 (from above, multiplying both sides of "a(ii, i+1) - a(ii, i) = -0.00004" by -1.) That times 100 is 0.004. Can 0.004/a(ii, i) be greater than 30? Let's take one value greater than 30 and see if we can find a value for the value that makes 0.004/x equal to it.
syms x
digits(5)
vpasolve(0.004/x == 31)
Is there something about your code that rules out a(ii, i) being equal to 0.0012903? [Of course, a simpler way to compute that would be]
0.004/31
Can other values of x satisfy 0.004/x > 30?
fplot(@(x) 0.004./x, [1e-5 1e-3])
yline(30, ':')
Any x where the solid line is above the dotted line would make that correction greater than 30.
x = 0.004/100 % makes the correction 100
figure
fplot(@(x) 0.004./x, [1e-5 1e-3])
yline(30, ':')
line(x, 100, Marker='o')
That marker looks pretty squarely on the blue curve to my eye.
Depending on what you're trying to do you should consider using a while statement (to keep adjusting the elements of the a vector while the condition is not satisfied instead of if the condition is not satisfied.)
4 个评论
Steven Lord
2024-5-24
Setting the corresponding elements of a to be 1 and 1+eps would satisfy the requirements you stated in your comments. But I'm guessing that isn't going to satisfy the requirements you haven't stated. What are those unstated requirements?
a1 = 1+eps;
a2 = 1;
Loss = (a2-a1)*100/a2
a1 is close to a2. a2 is smaller than a1.
isSmaller = a2 < a1
The corresponding Loss value is less than 30 (both in value and in absolute value.)
isLessThan30 = (Loss < 30) & (abs(Loss) < 30)
Are they an acceptable solution to your problem? Why or why not?
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!