Create Number and excluding

조회 수: 3 (최근 30일)
Kaizi
Kaizi 2023년 3월 22일
댓글: Steven Lord 2023년 3월 22일
hi everyone hope you can help me i know this request is very simple but i am new. I want to generate a sequence of numbers from 5 to 15 and remove numbers 9 and 13 using for and while . loops Thank you everyone
  댓글 수: 1
Steven Lord
Steven Lord 2023년 3월 22일
This sounds like a homework assignment because of your insistence on using for and/or while loops. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

댓글을 달려면 로그인하십시오.

채택된 답변

Antoni Garcia-Herreros
Antoni Garcia-Herreros 2023년 3월 22일
Hello,
Not sure why you would like to do this using a loop, but anyways:
l=5:15; % Array containing the values from 5 to 15
% you can always delete the elements doing l(5)=[];
i=1;
while i<length(l);
if l(i)==9;
l(i)=[];
elseif l(i)==13;
l(i)=[];
end
i=i+1;
end
ll=5:15; % Array containing the values from 5 to 15
lll=zeros(size(ll,2)-2,1); % Array where your result will be stored
j=1;
for i=1:length(ll)
if ll(i)==9 || ll(i)==13
continue
else
lll(j)=ll(i);
j=j+1;
end
end
  댓글 수: 1
Kaizi
Kaizi 2023년 3월 22일
thank you very much !

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2023년 3월 22일
No need for loops.
A=randi([5,15],1,100);
A(A==9|A==13)=[];
A
A = 1×83
12 5 7 15 12 6 8 8 10 15 6 12 8 8 6 8 11 6 8 15 6 14 11 10 7 11 6 8 7 12
  댓글 수: 2
Kaizi
Kaizi 2023년 3월 22일
thankyou!
Kaizi
Kaizi 2023년 3월 22일
if it is required to use for and while to generate a sequence of numbers in the order from 5 to 15 and excluding 9 and 13, how to do it?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by