How to get a vector using the following index. Please help me.
Show older comments
Hi guys, how to create the vector x = randperm(35)using only logical indexing:
y(x) = 2
if x < 6
y(x) = x - 4
if 6 <= x < 20
y(x) = 36 - x
if 20 <= x <= 35
where the curve should be a triangular shape, always above zero and with a maximum of 16. Im stuck with the my code where:
x=(1:35)
if(x<6)
y(x)=2
elseif(6<=x<20)
y(x)=x-4
elseif(20<=x<=35)
y(x)=36-x
end
plot(x,y)
But i cant seem to get a curve and get y to be right. is there a step where i missed?
2 Comments
p = randperm(n) returns a row vector containing a random permutation of the integers from 1 to n inclusive. In your case, randperm(35) "shuffles" integers from 1 to 35, so there must be some random number generation somewhere in your solution, .. or what did I misunderstand?
Amanda Kit Ting
on 6 Oct 2013
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!