Finding maximum value and its location in an array

조회 수: 9 (최근 30일)
Ana Mucalica
Ana Mucalica 2022년 3월 14일
답변: KSSV 2022년 3월 15일
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