Changing values in vector

Hello I have 1000 datas created by "randn" (A=randn(1,1000)). I want to change every value(n) which is not suitable for 1 < abs(A(n)) < 10. Also, new values should have been created by randn again. And it should provides the 1 < abs(A(n)) < 10 condition. So, I think have to create for loop for changing values until they provide the condition. How can I do that?

 采纳的回答

Setsuna Yuuki.
Setsuna Yuuki. 2020-11-16
编辑:Setsuna Yuuki. 2020-11-16
You can try this code
a = abs(randn(1,1000))
cont = 1;
while cont < length(a)+1
if(a(cont) < 1 || a(cont) > 10)
a(cont) = randn(1);
cont = 0;
end
cont = cont+1;
end

3 个评论

You changed all values positive before while loop. I don't need it. All I need is, changing each n value in A=randn(1,1000) which is not suitable for 1 < abs(A(n)) < 10. Until they are suitable ofc.
ohh i didn't see it, but it should work without abs()
Yes, I think it is okay. I arrange it a little bit. Thanks so much.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

版本

R2018a

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by