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
3 件のコメント
Mardan Tahmazli
2016 年 7 月 10 日
Mardan Tahmazli
2016 年 7 月 10 日
Mardan Tahmazli
2016 年 7 月 10 日
カテゴリ
ヘルプ センター および 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!