Finding difference of array using alternative indexes

5 次查看(过去 30 天)
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 个评论
shane watson
shane watson 2021-6-7
@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 2021-6-7
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.

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2021-6-7
编辑:Adam Danz 2021-6-7
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 个评论
shane watson
shane watson 2021-6-9
@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 2021-6-9
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.

请先登录,再进行评论。

更多回答(1 个)

KSSV
KSSV 2021-6-4
编辑:KSSV 2021-6-4
n = length(a) ;
iwant = (a(2:n)-a(1:n-1))./((2:n)-(1:n-1)) ;
Also have a look on geadient.
  4 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by