Apply operation to each element of an array

161 次查看(过去 30 天)
I have an array of numbers x1, x2, x3.... I want to obtain an array of numbers y1, y2, y3... such that yi = ceil((3/xi)^2). Is there a way to do this in one line of code without a for loop? I could do it with a for loop but I feel like I should be able to just apply the operation elementwise in a one line command.
Something like
Y = ciel((3/X)^2))
but of course that is not allowed

采纳的回答

Fabio Freschi
Fabio Freschi 2019-11-15
编辑:Fabio Freschi 2019-11-15
You should use element-wise operations
In your case
Y = ceil((3./X).^2))
  1 个评论
Adam Fitchett
Adam Fitchett 2019-11-15
编辑:Adam Fitchett 2019-11-15
Thank you. I wondered about using the dot for element wise, but didn’t realise I could use it twice in the same operation like that

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by