How to create a triangular shape using following Code in MATLAB

5 vues (au cours des 30 derniers jours)
Stephen john
Stephen john le 13 Juil 2022
Commenté : Stephen john le 18 Juil 2022
Hello everyone, i hope you are doing well. i want to generate triangular shape which contrain1000 images and which have length of 1000 samples.
I have write the following code to generate up and down shape of triangular as you see in image. but i want to combine them to generate a triangular shape
How can i do it in matalb
for z=1:1000
samples=1000;
x=(linspace(0, 10, samples));
maxvalue=round(rand*195)+5;
minvalue=round(rand*3)+2;
repeat=round(rand*8)+2;
xr = 0.01/repeat;
y = mod(x,xr)*(maxvalue - minvalue)/xr + minvalue;
y(:,1000)=y(:,999);
S(z,:)=y;
end
scatter(1:length(S(1,:)),S(1,:))
for z=1:1000
samples=1000;
x=(linspace(10, 0, samples));
maxvalue=round(rand*195)+5;
minvalue=round(rand*3)+2;
repeat=round(rand*8)+2;
xr = 0.01/repeat;
y = mod(x,xr)*(maxvalue - minvalue)/xr + minvalue;
y(:,1000)=y(:,999);
Slidingup(z,:)=y;
end

Réponses (1)

Karim
Karim le 13 Juil 2022
Modifié(e) : Karim le 13 Juil 2022
Based on the scatter mentioned in you question I combined the two loops and the resulting points in a single grid. This allows to plot the results with a single call to the scatter command. Is this what you looking for? I made a minor modification to the routing so that it returns lines of equal height.
samples = 1000;
S1 = zeros(1000,1000);
S2 = zeros(1000,1000);
maxVal = zeros(1000,1);
minVal = zeros(1000,1);
for z = 1:1000
maxVal(z) = round(rand*195)+5;
minVal(z) = round(rand*3)+2;
repeat = round(rand*8)+2;
xr = 0.01/repeat;
x = linspace(0, 10, samples);
y = mod(x,xr)*(maxVal(z) - minVal(z))/xr + minVal(z);
S1(z,:) = y;
x = linspace(10, 0, samples);
y = mod(x,xr)*(maxVal(z) - minVal(z))/xr + minVal(z);
S2(z,:) = y;
end
% gather the points in a single grid
Grid = [ (1:length(S1(1,:)))' S1(1,:)';
(1:length(S2(1,:)))' S2(1,:)'];
figure
scatter(Grid(:,1), Grid(:,2))
grid on
Edit, extra images after comment by the OP. Note that there are some odd points, that appear not to be connected. These are artefacts from the mod operation that generates the points. Have a good look at the algortihm that generates the points.
%% now gather all sides for the seperate 'triangles'
% since the lines are generated by random comands, we need a bit of logic to seperate them
repIdx = [0 find(diff(S1(1,:)) < 0) samples];
F1 = [];
F2 = [];
for i = 1:2:(numel(repIdx)-2)
F1 = [F1 S1(1,repIdx(i)+1:repIdx(i+1)) S2(1,(repIdx(i+1)+1):repIdx(i+2))];
F2 = [F2 S2(1,repIdx(i)+1:repIdx(i+1)) S1(1,(repIdx(i+1)+1):repIdx(i+2))];
end
F1 = [(1:numel(F1))' F1'];
% this gahters the points for triangle 2
F2 = [(1:numel(F2))' F2'];
figure
subplot(1,2,1)
scatter(F1(:,1), F1(:,2))
grid on
subplot(1,2,2)
scatter(F2(:,1), F2(:,2))
grid on
%% only gather points for the first 'triangle'
T1 = [(1:2*repIdx(2))' [S1(1,1:repIdx(2)) S2(1,(repIdx(2)+1:repIdx(3)))]'];
T2 = [(1:2*repIdx(2))' [S2(1,1:repIdx(2)) S1(1,(repIdx(2)+1:repIdx(3)))]'];
figure
subplot(1,2,1)
scatter(T1(:,1), T1(:,2))
grid on
subplot(1,2,2)
scatter(T2(:,1), T2(:,2))
grid on
  7 commentaires
Stephen john
Stephen john le 16 Juil 2022
@Karim Did you understand , i want to create a array of 1000x1000 using same code. in above F1 and F2 only single value array is store.
Stephen john
Stephen john le 18 Juil 2022
@Karim Can you please reply here?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Computer Vision with Simulink dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by