How to obtain a fixed-size array from a variable-size array

6 ビュー (過去 30 日間)
David Ramsey
David Ramsey 2018 年 5 月 3 日
コメント済み: Stephen23 2018 年 5 月 3 日
I'm using the Matlab Function block in Simulink and I have the following problem :
In my code, I have a two vectors : 'A0', of fixed-size and 'A1', of variable-size.
The size of 'A0' is 'n0', thus A0(n0,1) The maximum size of A1 is 'n0'. Thus, (Size of A1) <= (Size of A0).
One of the outputs of my Matlab Function is A = A0 + A1. Since it's a sum of vectors, they must have the same size, so what I did is fill 'A1' with zeros until reaching the same size. For exemple :
n0 = length(A0);
n1 = length(A1) ;
m = n0 - n1 ;
if n1 < n0
A1_1 = [A1 ; zeros(m,1)] ;
else
A1_1 = A1 ;
end
A = A0 + A1_1 ;
I believe that with this code, 'A', 'A1_1' and 'A0' will always have the same fixed size (since the reference, 'A0', is already fixed). In my simulation, I get the value of 'A' that I desire.
The problem is that for Simulink, 'A' is still a variable-size array, so I can't branch its signal directly with some other blocks.
What I need is 'A' (or even 'A1_1') to be read by Matlab-Simulink as a fixed-size array.

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 5 月 3 日
Because you are doing dynamic allocation,
A1_1 = [A1 ; zeros(m,1)] ;
MATLAB has no way of knowing what will be the final size of A1_1. To avoid such problem, try initializing A1_1 and other variables with a fixed maximum size
A1_1 = zeros(size(A0)); % or maximum size.
and then assign values to A1_1 using indexing. This way MATLAB will know in advance that what is the maximum size that these variables can take and thus consider then as fixed size array.
  2 件のコメント
David Ramsey
David Ramsey 2018 年 5 月 3 日
That seems to fix my problem, Thank you !!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by