Linspace function in for loop

7 views (last 30 days)
Yen Tien Yap
Yen Tien Yap on 16 Jul 2022
Answered: Star Strider on 16 Jul 2022
delta_l_2 = 1; % change in length of tendon 1 and tendon 2 (assume to be the same)
length_1_range = 10:-delta_l_2:5;
length_2_range = 20:delta_l_2:25;
[~,n] = size(length_1_range);
length_1 = zeros(100,n);
length_2 = zeros(100,n);
for i = 1:n
length_1(i) = linspace(0,length_1_range(i),100)'; % length of tendon 1
for j = 1:n
length_2(j) = linspace(0,length_2_range(j),100)'; % length of tendon 2
end
end
I want to create arrays for length 1 and length 2 with values range from 0 to each value in length_1_range and length_2_range but it came out errrors when I run it. May I ask how to correct this code? Thank you!

Accepted Answer

Star Strider
Star Strider on 16 Jul 2022
Try this —
delta_l_2 = 1; % change in length of tendon 1 and tendon 2 (assume to be the same)
length_1_range = 10:-delta_l_2:5;
length_2_range = 20:delta_l_2:25;
[~,n] = size(length_1_range);
length_1 = zeros(n,100);
length_2 = zeros(n,100);
for i = 1:n
length_1(i,:) = linspace(0,length_1_range(i),100)'; % length of tendon 1
for j = 1:n
length_2(j,:) = linspace(0,length_2_range(j),100)'; % length of tendon 2
end
end
length_1
length_1 = 6×100
0 0.1010 0.2020 0.3030 0.4040 0.5051 0.6061 0.7071 0.8081 0.9091 1.0101 1.1111 1.2121 1.3131 1.4141 1.5152 1.6162 1.7172 1.8182 1.9192 2.0202 2.1212 2.2222 2.3232 2.4242 2.5253 2.6263 2.7273 2.8283 2.9293 0 0.0909 0.1818 0.2727 0.3636 0.4545 0.5455 0.6364 0.7273 0.8182 0.9091 1.0000 1.0909 1.1818 1.2727 1.3636 1.4545 1.5455 1.6364 1.7273 1.8182 1.9091 2.0000 2.0909 2.1818 2.2727 2.3636 2.4545 2.5455 2.6364 0 0.0808 0.1616 0.2424 0.3232 0.4040 0.4848 0.5657 0.6465 0.7273 0.8081 0.8889 0.9697 1.0505 1.1313 1.2121 1.2929 1.3737 1.4545 1.5354 1.6162 1.6970 1.7778 1.8586 1.9394 2.0202 2.1010 2.1818 2.2626 2.3434 0 0.0707 0.1414 0.2121 0.2828 0.3535 0.4242 0.4949 0.5657 0.6364 0.7071 0.7778 0.8485 0.9192 0.9899 1.0606 1.1313 1.2020 1.2727 1.3434 1.4141 1.4848 1.5556 1.6263 1.6970 1.7677 1.8384 1.9091 1.9798 2.0505 0 0.0606 0.1212 0.1818 0.2424 0.3030 0.3636 0.4242 0.4848 0.5455 0.6061 0.6667 0.7273 0.7879 0.8485 0.9091 0.9697 1.0303 1.0909 1.1515 1.2121 1.2727 1.3333 1.3939 1.4545 1.5152 1.5758 1.6364 1.6970 1.7576 0 0.0505 0.1010 0.1515 0.2020 0.2525 0.3030 0.3535 0.4040 0.4545 0.5051 0.5556 0.6061 0.6566 0.7071 0.7576 0.8081 0.8586 0.9091 0.9596 1.0101 1.0606 1.1111 1.1616 1.2121 1.2626 1.3131 1.3636 1.4141 1.4646
length_2
length_2 = 6×100
0 0.2020 0.4040 0.6061 0.8081 1.0101 1.2121 1.4141 1.6162 1.8182 2.0202 2.2222 2.4242 2.6263 2.8283 3.0303 3.2323 3.4343 3.6364 3.8384 4.0404 4.2424 4.4444 4.6465 4.8485 5.0505 5.2525 5.4545 5.6566 5.8586 0 0.2121 0.4242 0.6364 0.8485 1.0606 1.2727 1.4848 1.6970 1.9091 2.1212 2.3333 2.5455 2.7576 2.9697 3.1818 3.3939 3.6061 3.8182 4.0303 4.2424 4.4545 4.6667 4.8788 5.0909 5.3030 5.5152 5.7273 5.9394 6.1515 0 0.2222 0.4444 0.6667 0.8889 1.1111 1.3333 1.5556 1.7778 2.0000 2.2222 2.4444 2.6667 2.8889 3.1111 3.3333 3.5556 3.7778 4.0000 4.2222 4.4444 4.6667 4.8889 5.1111 5.3333 5.5556 5.7778 6.0000 6.2222 6.4444 0 0.2323 0.4646 0.6970 0.9293 1.1616 1.3939 1.6263 1.8586 2.0909 2.3232 2.5556 2.7879 3.0202 3.2525 3.4848 3.7172 3.9495 4.1818 4.4141 4.6465 4.8788 5.1111 5.3434 5.5758 5.8081 6.0404 6.2727 6.5051 6.7374 0 0.2424 0.4848 0.7273 0.9697 1.2121 1.4545 1.6970 1.9394 2.1818 2.4242 2.6667 2.9091 3.1515 3.3939 3.6364 3.8788 4.1212 4.3636 4.6061 4.8485 5.0909 5.3333 5.5758 5.8182 6.0606 6.3030 6.5455 6.7879 7.0303 0 0.2525 0.5051 0.7576 1.0101 1.2626 1.5152 1.7677 2.0202 2.2727 2.5253 2.7778 3.0303 3.2828 3.5354 3.7879 4.0404 4.2929 4.5455 4.7980 5.0505 5.3030 5.5556 5.8081 6.0606 6.3131 6.5657 6.8182 7.0707 7.3232
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!