MATLAB Answers

Apply operation to each element of an array

조회 수: 80(최근 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

  댓글 수: 0

로그인 to comment.

채택된 답변

Fabio Freschi
Fabio Freschi 15 Nov 2019
Fabio Freschi 님이 편집함. 15 Nov 2019
You should use element-wise operations
In your case
Y = ceil((3./X).^2))

  댓글 수: 1

Adam Fitchett
Adam Fitchett 15 Nov 2019
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

로그인 to comment.

More Answers (0)

이 질문에 답변하려면 로그인을(를) 수행하십시오.





Translated by