How do I print only one element of a char array with same elements?
Show older comments
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
>>
Answers (1)
Azzi Abdelmalek
on 9 Jul 2016
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
3 Comments
Mardan Tahmazli
on 10 Jul 2016
Mardan Tahmazli
on 10 Jul 2016
Mardan Tahmazli
on 10 Jul 2016
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!