i want to change the result in the following code that instead of result 1 2 3 4 5 give me a b c d e
1 view (last 30 days)
Show older comments
for ii=1:(2000)
if ii>=1 && ii<=(400)
c(1,ii)=1;
else
if ii >=(401) && ii<=(800)
c(1,ii)=2;
else
if ii>=(801) && ii<=(1200)
c(1,ii)=3;
else
if ii>=(1201) && ii<=(1600)
c(1,ii)=4;
else
if ii>=(1601) && ii<=(2000)
c(1,ii)=5;
end
end
end
end
end
end
1 Comment
Answers (3)
Jan
on 29 May 2017
The question is not clear. What is "a b c d e"?
A bold guess:
S = 'abcde';
Result = S(c);
2 Comments
Jan
on 30 May 2017
[EDITED, Moved from Answer section]
Alsadegh Mohamed wrote:
Thank you Jan Simon but the result of my program is as following from 1 to 400 is 1 and i want the result is a ----- and from 4001 to 800 is 2 and I want 2 ----- and from 8001 to 1200 is 3 and I want c ---- and from 1201 to 1600 is 4 and I want d ---- and from 1601 to 2000 is 5 and I want c ... i can not but a instead of 1 and b instead of 2 ..... So how can i get a b c d I mean how can put (a) instead of (1) ... without error
Jan
on 30 May 2017
"from 4001 to 800 is 2 and I want 2"? Do you mean 'b'?
I still think that my suggestion works. Did you try it? What do you observe?
The question is still not clear. Please create a small example of what you want, using Matlab syntax - do not use 400 but 2 elements.
Guillaume
on 29 May 2017
Tip: a bunch of if ... else statements is rarely a good way to solve problems.
The most straightforward way to obtain your vector would be:
c = repelem('abcde', 400)
0 Comments
Alsadegh Mohamed
on 29 May 2017
1 Comment
Guillaume
on 29 May 2017
Please use comments rather than creating new answers every time.
What is (a)? the character 'a' or something else. If you don't use valid matlab syntax, it's impossible to understand what you want.
My answer gives you a vector where the elements 1 to 400 are 'a', 401 to 800 is 'b', etc. It does so efficiently.
See Also
Categories
Find more on Logical 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!