Creating a matrix with alternating and increasing values

1 回表示 (過去 30 日間)
Radhika Kulkarni
Radhika Kulkarni 2021 年 2 月 10 日
回答済み: Radhika Kulkarni 2021 年 2 月 10 日
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

採用された回答

Radhika Kulkarni
Radhika Kulkarni 2021 年 2 月 10 日
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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by