필터 지우기
필터 지우기

multiplication table using for loop

조회 수: 9 (최근 30일)
saphir alexandre
saphir alexandre 2022년 2월 17일
댓글: Walter Roberson 2022년 3월 7일
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

채택된 답변

Walter Roberson
Walter Roberson 2022년 2월 17일
n = 5;
sampleArray = zeros(1,6)
sampleArray = 1×6
0 0 0 0 0 0
for i = 0:n
sampleArray(i+1) = i * 2;
end
sampleArray
sampleArray = 1×6
0 2 4 6 8 10

추가 답변 (2개)

David Hill
David Hill 2022년 2월 17일

ANMOL Shakya
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
ANMOL Shakya
ANMOL Shakya 2022년 3월 7일
this code use as print table using for loop.
Walter Roberson
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by