フィルターのクリア

Function g Returns Only 2 Values Instead of Array - Vectorized Operation Issue in MATLAB

4 ビュー (過去 30 日間)
I'm encountering an issue with a vectorized function in MATLAB that's not returning the expected array of results. The function g and set of values of x2 is defined in the above. When I apply g to an array x2, I expect to get an array of transformed values. However, I only receive two values in the output array Z.
I don't think it's a format or display issue, as i've checked on the length and size of Z. Does anyone know what's wrong with this issue?
Thanks in advance for the assistance :)
  2 件のコメント
Stephen23
Stephen23 2024 年 4 月 16 日
編集済み: Stephen23 2024 年 4 月 16 日
"Does anyone know what's wrong with this issue?"
You defined an anonymous function with one input argument x2... but inside that function you completely ignored the input argument x2 and instead referred to the variable x that apparently exists in the workspace. When you check the size of x you will find that it has size 1x2.
Sheryl
Sheryl 2024 年 4 月 16 日
Ah, a silly mistake on my part. Thanks a lot!

サインインしてコメントする。

採用された回答

Alan Stevens
Alan Stevens 2024 年 4 月 16 日
You have g as a function of x2, but x2 doesn't appear on the right-hand side!

その他の回答 (1 件)

Rishi
Rishi 2024 年 4 月 16 日
Hi Sheryl,
I understand from your question that you want to know why is the length of the output from the anonymous function 'g' 2 even though the length of the vector 'x2' is 9.
That is because of the way you have defined the anonymous function 'g'. You have set the input argument to be 'x2', but inside the function, you perform the actions on some other variable 'x'. In this case, instead of taking the values from 'x2' in the function, it utilises some other variable 'x' fron your workspace.
You can redefine your function 'g' as below:
g = @(x) 2.*((sin(7.*x)./4)+((x.^2)./9)+1)
You can learn more about anonymous function from the below documentation:
Hope this helps!

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by