How i can add a car image to yellow boxes and at y coordinate 4,5,6 a road image? (Sorry i have low vocabulary of English).

1 view (last 30 days)
close all;
clc;
loop = 200;
r = 9;
c = 20;
carOccuranceMidWay = 0.05; %0-1
carMoveMidWay = 0.4; %0-1
carMoveFastWay = 0.7; %0-1
carMoveSlowWay = 0.2; %0-1
% define grid
x = zeros(r, c);
figure;
% x(5,1) = 1;
%
for i = 1:loop
for j = 1:c - 1
if(rand <= carOccuranceMidWay && x(5,1) == 0)
x(5,1) = 1;
end
% car exit from highway
if(x(5,c - 1) == 1) %For centerline
x(5,c - 1) = 0;
end
if(x(4,c - 1) == 1) %For FastLane
x(4,c - 1) = 0;
end
if(x(6,c - 1) == 1) %For SlowLane
x(6,c - 1) = 0;
end
% car move
if(x(5, j) == 1 && x(5, j + 1) == 0) % transition rules #1 for centerlane movement
if(rand <= carMoveMidWay)
x(5, j ) = 0;
x(5, j + 1) = 1;
end
end
if(x(5, j) == 1 && x(5, j + 1) == 1 && x(4,j)== 0) % transition rules #2 for transition to FastLane
if(rand <= carMoveMidWay)
x(5, j ) = 0;
x(4, j ) = 1;
end
end
if(x(5, j) == 1 && x(5, j + 1) == 1 && x(6,j)== 0) % transition rules #3 for transition to SlowLane
if(rand <= carMoveMidWay)
x(5, j ) = 0;
x(6, j ) = 1;
end
end
if(x(6, j) == 1 && x(6, j + 1) == 0) % transition rules #4 for SlowLane movement
if(rand <= carMoveSlowWay)
x(6, j ) = 0;
x(6, j + 1) = 1;
end
end
if(x(4, j) == 1 && x(4, j + 1) == 0) % transition rules #5 for FastLane movement
if(rand <= carMoveFastWay)
x(4, j ) = 0;
x(4, j + 1) = 1;
end
end
% Animate
clf; % Clear figure
imagesc(x);
% Display grid
pause(0.01);
end % Pause for 0.01 s
end

Answers (1)

Pramil
Pramil on 19 Aug 2024
Hi Abdul,
The “imagesc” function can only plot heatmaps, to add roads and cars to your animation you can explore the “plot(scenario)” functionality of MATLAB used for plotting driving scenario which is the case.
You can also investigate the “vehicle” object which is used to add vehicle to driving scenario.
Here are the links to the documentation for the above-mentioned functionalities:
Hope it helps.

Community Treasure Hunt

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

Start Hunting!