How to create a triangular shape using following Code in MATLAB

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

Answers (1)

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 Comments

@Karim in the last picture, we have two shape i want to save the shape seperatly. For example one is regular triangle and other is inverted triangle.
One start from x=0,y=0, while the other is start from x=0,y=150. they are two traingle i want to save in both seperate array
I made some adjustments to the code, to produce the images i think you are looking for.
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.
@Karim It just gives 1 image with 1000 samples. i want to generate 1000 images for 1000 samples.How can i procede to it.
What preciesly do you mean with a 1000 images?
You can do
numImages = 1000;
for i = 1:numImages
figure(i)
scatter( )
end
Which will create 1000 figures, however there is a good chance you computer will block (i.e. not respond for a while) if you do this.
@Karim like i want the array shape of 1000x1000. because as you seen in my above code i have created a up and down shape having arrayshape of 1000x1000. so thats why i want this to be 1000x1000.
Now the shape is 999x2. first coulmn is just number while other column is the values of triangle
@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.

Sign in to comment.

Products

Release

R2021b

Asked:

on 13 Jul 2022

Commented:

on 18 Jul 2022

Community Treasure Hunt

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

Start Hunting!