How to subtract ?

2 次查看(过去 30 天)
Arif Hoq
Arif Hoq 2022-9-16
移动Stephen23 2022-9-16
Its just a simple subtraction function in excel. Even It seems very simple in matlab. Still hitting my brain, but ....
A=[0 1 2 3 4 5 6];
B=12;
expected result of C: [11 9 6 2 -3 -9]
hints: 12-1 =11, 11-2=9, 9-3=6, 6-4=2, 2-5=-3, -3-6= -9
  3 个评论
Arif Hoq
Arif Hoq 2022-9-16
actually this command does not meet my code. later i got the way with a for loop.
Stephen23
Stephen23 2022-9-16
编辑:Stephen23 2022-9-16
"actually this command does not meet my code."
It gives exactly the same result as you request in your question.
And as I show here, using your MAT file data it gives exactly the same output as your more complex loop.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2022-9-16
移动:Stephen23 2022-9-16
The simple MATLAB approach:
S = load("allmat.mat");
M = S.allmat
M = 121×10
0 0 160.0000 80.0000 0 160.0000 80.0000 0 160.0000 80.0000 -1.3713 0 0 0 -0.4571 0 0 0 0 0 -1.6463 0 0 0 -0.5488 0 0 0 0 0 -0.9919 0 0 0 -0.3306 0 0 0 0 0 -0.8804 0 0 0 -0.2935 0 0 0 0 0 2.1362 0 0 0 0.7121 0 0 0 0 0 5.4360 0 0 0 1.8120 0 0 0 0 0 2.1003 0 0 0 0.7001 0 0 0 0 0 -7.7674 0 0 0 -2.5891 0 0 0 0 0 -5.1723 0 0 0 -1.7241 0 0 0 0 0
M(:,6) = M(1,6)-cumsum(M(:,5))
M = 121×10
0 0 160.0000 80.0000 0 160.0000 80.0000 0 160.0000 80.0000 -1.3713 0 0 0 -0.4571 160.4571 0 0 0 0 -1.6463 0 0 0 -0.5488 161.0059 0 0 0 0 -0.9919 0 0 0 -0.3306 161.3365 0 0 0 0 -0.8804 0 0 0 -0.2935 161.6300 0 0 0 0 2.1362 0 0 0 0.7121 160.9179 0 0 0 0 5.4360 0 0 0 1.8120 159.1059 0 0 0 0 2.1003 0 0 0 0.7001 158.4058 0 0 0 0 -7.7674 0 0 0 -2.5891 160.9949 0 0 0 0 -5.1723 0 0 0 -1.7241 162.7190 0 0 0 0

更多回答(3 个)

Steven Lord
Steven Lord 2022-9-16
A=[0 1 2 3 4 5 6];
B = 12;
C = B-cumsum(A)
C = 1×7
12 11 9 6 2 -3 -9
  1 个评论
Arif Hoq
Arif Hoq 2022-9-16
seems simple and faster. but i can't apply it in my code. I have attached a mat file.
a=load("allmat.mat");
allmat=a.allmat;
batBstorage=allmat(:,6)-cumsum(allmat(:,5));
allmat(:,6)=batBstorage
allmat = 121×10
0 0 160.0000 80.0000 0 160.0000 80.0000 0 160.0000 80.0000 -1.3713 0 0 0 -0.4571 0.4571 0 0 0 0 -1.6463 0 0 0 -0.5488 1.0059 0 0 0 0 -0.9919 0 0 0 -0.3306 1.3365 0 0 0 0 -0.8804 0 0 0 -0.2935 1.6300 0 0 0 0 2.1362 0 0 0 0.7121 0.9179 0 0 0 0 5.4360 0 0 0 1.8120 -0.8941 0 0 0 0 2.1003 0 0 0 0.7001 -1.5942 0 0 0 0 -7.7674 0 0 0 -2.5891 0.9949 0 0 0 0 -5.1723 0 0 0 -1.7241 2.7190 0 0 0 0
but the column 6 would be

请先登录,再进行评论。


Paul
Paul 2022-9-16
编辑:Paul 2022-9-16
Can do this in a loop
B = 12;
A = [0 1 2 3 4 5 6];
C = 0*A;
C(1) = B(1) - A(1);
for ii = 2:numel(A)
C(ii) = C(ii-1) - A(ii);
end
C = C(2:end)
C = 1×6
11 9 6 2 -3 -9
Or with a recursive filter
C = filter(-1,[1 -1],A,B);
C = C(2:end)
C = 1×6
11 9 6 2 -3 -9
  1 个评论
Arif Hoq
Arif Hoq 2022-9-16
编辑:Arif Hoq 2022-9-16
thank you very much.
a=load("allmat.mat");
allmat=a.allmat;
C(1) = allmat(1,6) - allmat(1,5);
for i = 2:120
C(i) = allmat(i-1,6) - allmat(i,5);
end
C=C';
allmat(2:end,6)=C
allmat = 121×10
0 0 160.0000 80.0000 0 160.0000 80.0000 0 160.0000 80.0000 -1.3713 0 0 0 -0.4571 160.0000 0 0 0 0 -1.6463 0 0 0 -0.5488 160.4571 0 0 0 0 -0.9919 0 0 0 -0.3306 0.5488 0 0 0 0 -0.8804 0 0 0 -0.2935 0.3306 0 0 0 0 2.1362 0 0 0 0.7121 0.2935 0 0 0 0 5.4360 0 0 0 1.8120 -0.7121 0 0 0 0 2.1003 0 0 0 0.7001 -1.8120 0 0 0 0 -7.7674 0 0 0 -2.5891 -0.7001 0 0 0 0 -5.1723 0 0 0 -1.7241 2.5891 0 0 0 0
but the column 6 would be

请先登录,再进行评论。


Arif Hoq
Arif Hoq 2022-9-16
编辑:Arif Hoq 2022-9-16
At last got the solution !!!!
a=load("allmat.mat");
allmat=a.allmat;
for i=1:size(allmat,1)
allmat(i,11)=allmat(i,6)-allmat(i,5);
allmat(i+1,6)=allmat(i,11);
allmat(i+1,11)=allmat(i+1,6)-allmat(i+1,5);
end
allmat(:,6)=allmat(:,11)
allmat = 122×11
0 0 160.0000 80.0000 0 160.0000 80.0000 0 160.0000 80.0000 160.0000 -1.3713 0 0 0 -0.4571 160.4571 0 0 0 0 160.4571 -1.6463 0 0 0 -0.5488 161.0059 0 0 0 0 161.0059 -0.9919 0 0 0 -0.3306 161.3365 0 0 0 0 161.3365 -0.8804 0 0 0 -0.2935 161.6300 0 0 0 0 161.6300 2.1362 0 0 0 0.7121 160.9179 0 0 0 0 160.9179 5.4360 0 0 0 1.8120 159.1059 0 0 0 0 159.1059 2.1003 0 0 0 0.7001 158.4058 0 0 0 0 158.4058 -7.7674 0 0 0 -2.5891 160.9949 0 0 0 0 160.9949 -5.1723 0 0 0 -1.7241 162.7190 0 0 0 0 162.7190
  1 个评论
Arif Hoq
Arif Hoq 2022-9-16
nice. thank you so much. It's better to use vectorization rather than loop.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by