Finding difference of array using alternative indexes

5 views (last 30 days)
Hello everyone,
I hope you're doing, I've simple question I've array (1, 24), now I want to findout the difference and divide, like [element1-element2 element2- element3 element3-element4...............element24-element23], what I did as follows
for i=1:24
a=1x24
%first case
a1(i)=a(i)-a(i+1)./i-(i+1),
% Second case
a1(i)=a(i)-a(i-1)./i-(i-1)
end
However, it is clear the index causing error (first case: Index exceeds the number of array elements (24). second case: Array indices must be positive integers or logical values.)
, could you please help me out in this situation.
  6 Comments
shane watson
shane watson on 7 Jun 2021
@Adam Danz I'm sorry for the late response, however, the issue was "reduction of index and value" so in my case I need 1 x 24 matix at the end while it is reduces to 23, and your given last two steps are good but the it creates the same problem.
Adam Danz
Adam Danz on 7 Jun 2021
I'm trying to show you that the loss of one value when numerically differentiating is not a problem - it's exactly the expected behavior. Carefully look at my previous comment again to understand why you're losing a value.
I'll add an answer to suggest an alternative.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 7 Jun 2021
Edited: Adam Danz on 7 Jun 2021
The loss of 1 value when differentiating with diff(x,1) is the expected behavior. This function computes the difference between adjacent values in a matrix [a b c d] and there is n-1 comparisons.
Perhaps you're looking for the numeric gradient.
Comparison between gradient and diff
y = exp([1:.1:3]);
d = diff(y);
g = gradient(y);
size(y)
ans = 1×2
1 21
size(d)
ans = 1×2
1 20
size(g)
ans = 1×2
1 21
x = 1:numel(y)
x = 1×21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
hold on
plot(x(2:end),d,'b-','DisplayName','diff')
plot(x,g,'r-','DisplayName','gradient')
legend
  2 Comments
shane watson
shane watson on 9 Jun 2021
@Adam Danz, Thanks for the detail answer, I understood what you mentioned about loss of 1 value when differentiating, however as you showed the comparision of diff and gradient almost same, it is difficult to digest, espically in case when i'm computing the load demand of households and any single value changes it is problem for me. For example I used your example with sightly different values of "y" then the graph seems pretty different incase of diff and gradient.
ni=[6 7 8 9 7 5 5 6 7 8];
d = diff(ni);
g = gradient(ni);
x = 1:numel(ni);
hold on
plot(x(2:end),d,'b-','DisplayName','diff')
plot(x,g,'r-','DisplayName','gradient')
legend
Adam Danz
Adam Danz on 9 Jun 2021
I need to know more about your goal. In your original question, the indexing error was caused by the loss of 1 value due to differentiating using diff(). Maybe you don't need to differentiate. Maybe you need to differentiate using a different method. Or maybe what you're doing is fine and you need to understand the output.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 4 Jun 2021
Edited: KSSV on 4 Jun 2021
n = length(a) ;
iwant = (a(2:n)-a(1:n-1))./((2:n)-(1:n-1)) ;
Also have a look on geadient.
  4 Comments
shane watson
shane watson on 4 Jun 2021
Edited: shane watson on 4 Jun 2021
@KSSVIt's not showing the right results unfortunately. :(

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!