How to subtract ?

조회 수: 3(최근 30일)
Arif Hoq
Arif Hoq 2022년 9월 16일
Moved: 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
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일
Moved: 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.

댓글을 달려면 로그인하십시오.

범주

Find more on Loops and Conditional Statements 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!

Translated by