How to obtain a fixed-size array from a variable-size array
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Ameer Hamza
il 3 Mag 2018
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Performance and Memory in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!