Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to impliment this without loop?

조회 수: 1 (최근 30일)
Dimitrios
Dimitrios 2014년 11월 26일
마감: MATLAB Answer Bot 2021년 8월 20일
I need to take the rate of change of a variable and for the first value there is not a previous value to take.For the initial i assumed that the rate of change is zero as the simulation is so long that it will not affect the results.So i tried something like this:
obj.Value(indexT,BladeN) = function(X,Y);
PreviousValue = obj.Value(indexT-1,BladeN)*(index~=1)+(index==1)*obj.Value(indexT,BladeN);
rate = (obj.Value(indexT,BladeN) - PreviousValue)/timestep
Ofcurse its not running cause there is no zero index.I know it can be done by a loop but I would prefer something more elegant.Any idea?

답변 (1개)

Henrik
Henrik 2014년 11월 27일
You example is a bit confusing to me, there seems to be several unnecessary complications, e.g. using fields.
Anyway, here's what I would do:
rate=(obj.Value(2:end,BladeN)-obj.Value(1:end-1,BladeN))/timestep;
I'd even guess that this will work, depending on exactly what you need.
rate=(obj.Value(2:end,:)-obj.Value(1:end-1,:))/timestep;
If you want rate to be the same size as obj.Value, you could also do
rate=zeros(size(obj.Value));
rate(2:end,:)=(obj.Value(2:end,:)-obj.Value(1:end-1,:))/timestep;
I hope this can be used?
You can also look up diff, that might be helpful.

Community Treasure Hunt

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

Start Hunting!

Translated by