How to express inner product of two vectors in Stateflow action language?

3 ビュー (過去 30 日間)
Wilfred
Wilfred 2011 年 11 月 11 日
I want to compute the inner product y of two vectors u and v (same size, both based on input from Simulink) in Stateflow action language. I have tried
x=ml.transpose(u)
y=ml.(x*v)
but during compilation this results in a coder error "Cannot reshape Local Data 'x' (#...) ([...]) to Local Data 'y' (#...) (scalar) for operator =. Please refer to object (#...)." Apparently, the "ml." notation only works with functions, not with operators such as '*'. Would there be a way to write a product as a function - something like y=ml.product(x,v)?
An alternative might be
x=ml.transpose(u)
y=ml('x*v')
but this requires x and v to be defined in the Matlab workspace, which would mean I have to export them first. This seems to be a rather laborious solution.
Any ideas?
  1 件のコメント
Wilfred
Wilfred 2011 年 11 月 14 日
I should have mentioned that both u and v are composed of integers. However, it seems that solutions are not available in that case, let's assume that u and v are composed of real numbers, as integers can of course also be converted to real numbers.

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

採用された回答

Kaustubha Govind
Kaustubha Govind 2011 年 11 月 11 日
Try the function times for inner product of two vectors.
  2 件のコメント
Wilfred
Wilfred 2011 年 11 月 14 日
This was not exactly what I was looking for, but it helped me solve my problem. The function "times" multiplies input arrays element by element and puts these in a vector the same size as the input vectors (with no need for transposing one of them as I initially tried). What I wanted was a scalar equal to the sum of the element-by-element multiplications, which can of course be computed from it:
y=ml.sum(ml.times(u,v))
Wilfred
Wilfred 2011 年 11 月 14 日
And, by the way, Thanks!!!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 11 月 11 日
The MATLAB * operator has the formal name mtimes
  1 件のコメント
Wilfred
Wilfred 2011 年 11 月 14 日
Thanks, this works too, if put this way:
y=ml.mtimes(ml.transpose(u),v)
Here, the order of listing the arguments is important.

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

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by