Finding maximum value and its location in an array

10 次查看(过去 30 天)
I am working with a code in matlab that simulates a wave travelling in time. The following picture is my output at time zero.
I need to plot the amplitude of the wave as a function of time, since the wave gets smaller as it interacts with the ramp. To do this it's been suggested to do a loop that goes through the array and compares two values u_j+1 and u_j to check if u_j+1<u_j. This is a sample of my code, where u is a column vector.
for j = 2 : length(t)
diff_first=A*u1/(2*h);
sum_w = B*u1;
diff_middle=2*diff_first.*sum_w;
diff_third=A_third*u1/(2*h^3);
if j == 2
u1 = u - dt*(diff_middle+diff_third); %Computing the first step
figure(1); plot(x,u1,'r.');
else
u2 = u - 2*dt*(diff_middle+diff_third); %Computing further steps
figure(1); plot(x,u2,'r.');
u = u1; u1 = u2;
end
hold on; axis([x(1) x(end) -1 Am]); hold off;
end

回答(1 个)

KSSV
KSSV 2022-3-15
REad about the function max. If v is your array.
[val,idx] = max(v)

类别

Help CenterFile Exchange 中查找有关 Matched Filter and Ambiguity Function 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by