How do I print only one element of a char array with same elements?
Afficher commentaires plus anciens
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
>>
Réponses (1)
Azzi Abdelmalek
le 9 Juil 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 commentaires
Mardan Tahmazli
le 10 Juil 2016
Mardan Tahmazli
le 10 Juil 2016
Mardan Tahmazli
le 10 Juil 2016
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!