appending element to vector

조회 수: 5 (최근 30일)
shobhit mehrotra
shobhit mehrotra 2015년 2월 4일
댓글: Guillaume 2015년 2월 4일
Hello Im a diff function to estimate a derivative(du/dx), however I lose an element as excepted, now I want to add the average value of the derivative to the end of vector (du/dx) such that is the same length as u. Thanks

답변 (2개)

Guillaume
Guillaume 2015년 2월 4일
That doesn't sound very sensible as you're mixing two different entities into a single variable, but if that's what you want to do:
x = sin(linspace(0, pi, 100)); %for example
d = diff(x);
dwithmean = [d mean(d)]
  댓글 수: 2
shobhit mehrotra
shobhit mehrotra 2015년 2월 4일
i have this script
for n = 1:length(dataind)
winddivergencex(n)= (diff(Ui(min(dataind{n}): (max(dataind{n})))))./(diff(longm(min(dataind{n}):(max(dataind{n})))));
winddivergencex has to contain the same number of elements as n so i want append a term to the end of the winddivergencex vector so they have the same amount of elements. Thanks
Guillaume
Guillaume 2015년 2월 4일
Why do they have to have the same number of elements?

댓글을 달려면 로그인하십시오.


Michael Haderlein
Michael Haderlein 2015년 2월 4일
As Guillaume has mentioned, that's not the usual way to go. Assume you want to plot the data (which requires equally sized arrays), these are the common options:
x=linspace(0,100);
y=x.^2;
dydx=diff(y)./diff(x);
figure, hold all
%best way: shift the x by half dx
x2=x(1:end-1)+diff(x)/2;
plot(x2,dydx)
%lazy way: just use n-1 x values
plot(x(1:end-1),dydx)
%semi-lazy way: add nan (will not be plotted)
dydx=[diff(y)./diff(x) nan];
plot(x,dydx)
When you run these lines, you see a shift between the blue line (which is technically correct) and the red/green lines (overlaying).

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by