Creating a matrix with alternating and increasing values

2 views (last 30 days)
My goal is to create a 17 x 75 matrix similar to the Excel image below. The first column remains all 0's. Starting from second column, the even columns begin with 0's and the odd columns begin with delta_x (0.2). The pattern continues with 0.2 added to odd and even columns. I believe I have to use a lot of nested for loops and if statements, but I am not getting the results I want. I was able to get the first 3 columns using the code below but am not sure where to go from here:
x_coor = zeros(17,75);
delta_x = 0.2; %cm
for i=1:17
if mod(i,2) == 0
x_coor(i,2) = delta_x/2;
else
x_coor(i,2) = 0;
end
end
for j=1:17
if mod(j,2) == 0
x_coor(j,3) = 0;
else
x_coor(j,3) = delta_x;
end
end
for col=4:75
if mod(col,2) == 0
for row=1:17
if mod(row,2) == 0
x_coor(row,col) = x_coor(row,col-2) + delta_x;
end
end
else
for row1 = 1:17
if mod(row1,2) ~= 0
x_coor(row,col) = x_coor(row,col-2) + delta_x;
end
end
end
end
**updated code: My even rows look fine; however, my odd rows are still not correct

Accepted Answer

Radhika Kulkarni
Radhika Kulkarni on 10 Feb 2021
I was able to figure it out:
x_coor = zeros(17,75);
delta_x = 0.2; %cm
for i=1:17
if mod(i,2) == 0
x_coor(i,2) = delta_x/2;
else
x_coor(i,2) = 0;
end
end
for j=1:17
if mod(j,2) == 0
x_coor(j,3) = 0;
else
x_coor(j,3) = delta_x;
end
end
for col=4:75
if mod(col,2) == 0
for row=1:17
if mod(row,2) == 0
x_coor(row,col) = x_coor(row,col-2) + delta_x;
end
end
elseif mod(col,2) ~= 0
for row1 = 1:17
if mod(row1,2) ~= 0
x_coor(row1,col) = x_coor(row1,col-2) + delta_x;
end
end
end
end

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!