How can I make the random point moves based on locations ?
조회 수: 1 (최근 30일)
이전 댓글 표시
Below there is a point randomly deployed, its moved based on the angles which leads to generate or deply that point in 5 locations according to the number of angles. My question how can I store these 5 positions in a matrix OR can I make index 5 positions in order to move that random point based on position ?? NOTE: The 5 positions that I'm asking about are " newPos " that mentioned in the code below.
Thanks in advance and appreciate any comment
%pointPos = [0 0];
v=2;%7.2/3.6;
%x=plot(pointPos,'*');
NumDrone1=1;center=[0 0];ro1=1000;
theta_Drone1=2*pi*(rand(NumDrone1,1));
g1 = 0.1 * ro1 + 0.1 * ro1 * rand(NumDrone1,1);
PosPoint1_x=center(1)+g1.*cos(theta_Drone1); % Initial positions
PosPoint1_y=center(2)+g1.*sin(theta_Drone1);
PosPoint1 = [PosPoint1_x ,PosPoint1_y]
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
h(1) = plot(PosPoint1(1,1),PosPoint1(1,2),'Parent', hax,'Marker', '.','Color', 'k','LineStyle', '-','MarkerSize', 12);
hold(hax, 'on')
grid(hax, 'on')
axis(hax, 'equal')
angleOption = [24 56 72 36 96]
for ii = 1:numel(angleOption)
chooseAngle = angleOption(ii)
displacement = [cos(chooseAngle).*v, sin(chooseAngle).*v];
newPos = PosPoint1 + displacement
startpos = newPos-displacement
XData = [h.XData newPos(:, 1)];
YData = [h.YData newPos(:, 2)];
set(h, 'XData', XData, 'YData', YData)
drawnow
XData = [h.XData startpos(:, 1)];
YData = [h.YData startpos(:, 2)];
set(h, 'XData', XData, 'YData', YData)
drawnow
end
댓글 수: 0
답변 (1개)
Image Analyst
2022년 12월 30일
I didn't dig in to your code but it sounds like you want to do a random walk.
See my collection of random walk demos.
To store values in an array you need to index them with the loop counter, like
XData(ii) = newPos(:, 1);
YData(ii) = newPos(:, 2);
and don't do that second set of assignments to XData and YData.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!