How do I print only one element of a char array with same elements?
이전 댓글 표시
So, here is the very simple practice code:
clear all
close all
clc
x=1:20;
for i=1:20
if x(i)<=5
say=('Hi')
elseif x(i)>5 && x(i)<=10
say=('Hello')
elseif x(i)>10 && x(i)<=15
say=('How is it going?')
else
say=('Fine')
end
end
I am new to Matlab and I got stuck on a simple point where I need to print any of the answers only once. I know that I'm gonna get 4 different answers 5 times each, but I want to get only one of each answer. One which will stand for the rest of the same answers. For example, just: 'Hi', 'Hello', 'How is it going', 'Fine' instead of getting each of them 5 times. Thanks in advance!
say =
Hi
say =
Hi
say =
Hi
say =
Hi
say =
Hi
say =
Hello
say =
Hello
say =
Hello
say =
Hello
say =
Hello
say =
How is it going?
say =
How is it going?
say =
How is it going?
say =
How is it going?
say =
How is it going?
say =
Fine
say =
Fine
say =
Fine
say =
Fine
say =
Fine
>>
답변 (1개)
Azzi Abdelmalek
2016년 7월 9일
x=1:20;
[test1,test2,test3,test4]=deal(0)
for i=1:20
if x(i)<=5 & test1==0
say=('Hi')
test1=1;
elseif x(i)>5 && x(i)<=10 & test2==0
say=('Hello')
test2=1;
elseif x(i)>10 && x(i)<=15 & test3==0
say=('How is it going?')
test3=1;
elseif test4==0
say=('Fine')
test4=1;
end
end
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!