MATLAB: Substracting previous value in a matrix and assigning it to a new matrix

cellfor loopsubtraction

Hi everyone,
I Would like some help doing the following; substracting the previous value in a cell except the first value.
Lets say we have : a=[1 5 3 6 4] , then i would like the output : b= [1 4 2 3 2].
so in the iteration it would be :
  • 1-0 = 1
  • 5-1= 4
  • 3-6 = 3
  • 6-4 = 2
The code works – the only problem is that it does not perform this operation for the first column
a=cell2mat(SortVal)
b=a;
for jj = 2:18
for ii = 2:8
b(jj,ii) = b(jj,ii) – a(jj-1,ii)
end
end
This is a:
a =
1.6766 0.6118 0.3707 0.0771 0.0010 0.2057 0.1219 0.2438
2.2065 0.8679 0.4766 0.0879 0.0017 0.3461 0.1624 0.3007
2.4282 1.0790 0.5340 0.0921 0.0019 0.4416 0.2042 0.3431
2.5295 1.2192 0.5736 0.1017 0.0028 0.4911 0.2265 0.3896
2.6850 1.3342 0.6188 0.1062 0.0032 0.5578 0.2483 0.4327
2.8442 1.3778 0.6719 0.1118 0.0039 0.5993 0.2736 0.4536
3.0451 1.4608 0.7491 0.1133 0.0055 0.6738 0.2959 0.4860
3.1747 1.5049 0.7910 0.1138 0.0062 0.7108 0.3170 0.5029
3.2891 1.5334 0.8172 0.1208 0.0062 0.7247 0.3449 0.5181
3.4065 1.5708 0.8959 0.1238 0.0064 0.7447 0.3641 0.5260
3.4989 1.6455 0.9100 0.1323 0.0064 0.7649 0.3892 0.5480
3.5645 1.6464 0.9129 0.1342 0.0065 0.7828 0.4151 0.5662
3.6503 1.6760 0.9917 0.1364 0.0074 0.7949 0.4171 0.5739
3.7089 1.7567 1.0135 0.1465 0.0077 0.8090 0.4371 0.5834
3.7803 1.7714 1.0432 0.1493 0.0083 0.8243 0.4580 0.5987
3.8278 1.7844 1.0745 0.1540 0.0085 0.8515 0.4608 0.6102
3.8642 1.8361 1.0975 0.1552 0.0114 0.8629 0.4871 0.6260
3.8935 1.8419 1.1220 0.1601 0.0115 0.8665 0.4877 0.6325
and this is my output
b =
1.6766 0.6118 0.3707 0.0771 0.0010 0.2057 0.1219 0.2438
2.2065 0.2560 0.1059 0.0109 0.0007 0.1404 0.0406 0.0569
2.4282 0.2112 0.0574 0.0042 0.0002 0.0955 0.0417 0.0424
2.5295 0.1401 0.0397 0.0096 0.0008 0.0496 0.0224 0.0465
2.6850 0.1150 0.0451 0.0045 0.0004 0.0667 0.0218 0.0431
2.8442 0.0437 0.0532 0.0056 0.0008 0.0415 0.0253 0.0209
3.0451 0.0830 0.0771 0.0014 0.0016 0.0745 0.0222 0.0323
3.1747 0.0441 0.0419 0.0005 0.0007 0.0370 0.0211 0.0170
3.2891 0.0285 0.0262 0.0070 0.0000 0.0139 0.0279 0.0152
3.4065 0.0374 0.0788 0.0030 0.0001 0.0199 0.0192 0.0079
3.4989 0.0747 0.0141 0.0086 0.0000 0.0202 0.0251 0.0219
3.5645 0.0008 0.0029 0.0018 0.0001 0.0179 0.0259 0.0182
3.6503 0.0296 0.0788 0.0022 0.0009 0.0121 0.0020 0.0077
3.7089 0.0808 0.0217 0.0101 0.0003 0.0140 0.0200 0.0095
3.7803 0.0146 0.0297 0.0028 0.0006 0.0153 0.0209 0.0153
3.8278 0.0130 0.0313 0.0048 0.0002 0.0272 0.0028 0.0115
3.8642 0.0517 0.0230 0.0011 0.0029 0.0113 0.0263 0.0158
3.8935 0.0059 0.0244 0.0049 0.0002 0.0036 0.0006 0.0065
The code does it for all the column, but not the first?

Best Answer

a = [
1.6766 0.6118 0.3707 0.0771 0.0010 0.2057 0.1219 0.2438
2.2065 0.8679 0.4766 0.0879 0.0017 0.3461 0.1624 0.3007
2.4282 1.0790 0.5340 0.0921 0.0019 0.4416 0.2042 0.3431
2.5295 1.2192 0.5736 0.1017 0.0028 0.4911 0.2265 0.3896
2.6850 1.3342 0.6188 0.1062 0.0032 0.5578 0.2483 0.4327
2.8442 1.3778 0.6719 0.1118 0.0039 0.5993 0.2736 0.4536
3.0451 1.4608 0.7491 0.1133 0.0055 0.6738 0.2959 0.4860
3.1747 1.5049 0.7910 0.1138 0.0062 0.7108 0.3170 0.5029
3.2891 1.5334 0.8172 0.1208 0.0062 0.7247 0.3449 0.5181
3.4065 1.5708 0.8959 0.1238 0.0064 0.7447 0.3641 0.5260
3.4989 1.6455 0.9100 0.1323 0.0064 0.7649 0.3892 0.5480
3.5645 1.6464 0.9129 0.1342 0.0065 0.7828 0.4151 0.5662
3.6503 1.6760 0.9917 0.1364 0.0074 0.7949 0.4171 0.5739
3.7089 1.7567 1.0135 0.1465 0.0077 0.8090 0.4371 0.5834
3.7803 1.7714 1.0432 0.1493 0.0083 0.8243 0.4580 0.5987
3.8278 1.7844 1.0745 0.1540 0.0085 0.8515 0.4608 0.6102
3.8642 1.8361 1.0975 0.1552 0.0114 0.8629 0.4871 0.6260
3.8935 1.8419 1.1220 0.1601 0.0115 0.8665 0.4877 0.6325];
b = diff(a,1,1)
b = 17×8
0.5299 0.2561 0.1059 0.0108 0.0007 0.1404 0.0405 0.0569 0.2217 0.2111 0.0574 0.0042 0.0002 0.0955 0.0418 0.0424 0.1013 0.1402 0.0396 0.0096 0.0009 0.0495 0.0223 0.0465 0.1555 0.1150 0.0452 0.0045 0.0004 0.0667 0.0218 0.0431 0.1592 0.0436 0.0531 0.0056 0.0007 0.0415 0.0253 0.0209 0.2009 0.0830 0.0772 0.0015 0.0016 0.0745 0.0223 0.0324 0.1296 0.0441 0.0419 0.0005 0.0007 0.0370 0.0211 0.0169 0.1144 0.0285 0.0262 0.0070 0 0.0139 0.0279 0.0152 0.1174 0.0374 0.0787 0.0030 0.0002 0.0200 0.0192 0.0079 0.0924 0.0747 0.0141 0.0085 0 0.0202 0.0251 0.0220