Array inside a function

1 次查看(过去 30 天)
carlos g
carlos g 2017-7-13
评论: Jan 2017-7-13
I have the following function
U2=@(eta) i/alpha2*DV2(eta)-beta2/alpha2*W2VEC;
where W2VEC is a vector of N components and alpha2,beta2 are just numbers.
I would like to be able to evaluate U2 at N different eta, which correspond to the N components of W2VEC as well.
The way I am doing it leads to a NxN vector (every eta multiplies a N-component vector), but this is not what I want. I would like that each eta would correspond to a component of W2VEC.
Is it possible to do this having W2VEC declared already as an array of N given components? When I do U2(0), for example, I still get a vector. And what I want to obtain is a single value (corresponding to DV2(0), which is OK, and the first component of W2VEC, which fails). I know how to do this manually (using a loop and redefining U2 each time), but I would like to do it in a more sustainable way. Is that possible?
Conversely, even if W2VEC is a numeric array, is it possible to convert it to a kind of "function" so that I can access each component through the argument? (An therefore, being able to introduce this new "function" into U2)
  2 个评论
Adam
Adam 2017-7-13
You need to use component-wise operations .* and ./ rather than matrix based * and /
I'm not sure I fully understand why U2(0) still produces a vector output, but you haven't told us exactly what the sizes are of all the components and what DV2 is. Is it an array or a function?
Jan
Jan 2017-7-13
@carlos g: It will be much easier to control the behavior of your function if you move it to a function, instead of the anonymous function in a one-liner.
W2VEC is a numeric array, is it possible to convert it to a kind
of "function" so that I can access each component through the
argument?
This sounds like you want to use an index of the array.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by