multiplication table using for loop
15 次查看(过去 30 天)
显示 更早的评论
hello,
I'm trying to create a multiplication table where numbers from 0 to 5 are times by 2
here is what i have:
n = 5;
sampleArray = zeros(1,6)
for i = 1:n
sampleArray(i) = i * 2
end
with this code my zeros array becomes [ 2,4,6,8,10,0], but i want the 0 to be the first value to get [0,2,4,6,8,10]. When i put i = 0:n, i get an error
please help
0 个评论
采纳的回答
Walter Roberson
2022-2-17
n = 5;
sampleArray = zeros(1,6)
for i = 0:n
sampleArray(i+1) = i * 2;
end
sampleArray
更多回答(2 个)
ANMOL Shakya
2022-3-7
编辑:Walter Roberson
2022-3-7
clc;
clear all;
close all;
N= 1:10;
f = zeros(size(N));
for i = 1:length(N);
f(i)= 17*N(i);
end
table1 = [N' f']
2 个评论
Walter Roberson
2022-3-7
Could you expand on why displaying a multiplication table should involve deleting all current graphics ( close all ) or deleting all current variables ( clear all ) ?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!