Finding difference of array using alternative indexes

4 Ansichten (letzte 30 Tage)
shane watson
shane watson am 4 Jun. 2021
Kommentiert: Adam Danz am 9 Jun. 2021
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 Kommentare
shane watson
shane watson am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 7 Jun. 2021
Bearbeitet: Adam Danz am 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 Kommentare
shane watson
shane watson am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 4 Jun. 2021
Bearbeitet: KSSV am 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 Kommentare
shane watson
shane watson am 4 Jun. 2021
Bearbeitet: shane watson am 4 Jun. 2021
@KSSVIt's not showing the right results unfortunately. :(

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by