Effacer les filtres
Effacer les filtres

Array Problem How can I unpack an array? why the firt element pf my array is always 0?

16 vues (au cours des 30 derniers jours)
Imp
Imp le 30 Oct 2023
Hi
I have written this code, I do not know why always the first element of the array I (which is I1=I(1) is Zero?
Here is the function that I have written the code.
function [I1,I2,I3] = DynamicModel(x1,x2,x3,V1,V2,V3,c1,c2,c3,Gs,G)
I_11 = 2*V1*c1*(Gs+G)-V2*c1*(Gs+G)-V3*c1*(Gs+G)+c1*c2*x2*(V1-V2)+c1*c3*x3*(V1-V3);
I_12 = V1*c2*(Gs+2*G)-V2*c2*(Gs+G)-V3*c2*G;
I_13 = V1*c3*(Gs+2*G)-V2*c3*G-V3*c3*(Gs+G);
I_21 = -V1*c1*(Gs+G)+V2*c1*(Gs+2*G)-V3*c1*G;
I_22 = -V1*c2*(Gs+G)+2*V2*c2*(Gs+G)-V3*c3*(Gs+G)+c2*c3*x3*(V2-V3)+c1*c2*x1*(V2-V1);
I_23 = -V1*c3*G- V2*c3*(Gs+2*G)-V3*c3*(Gs+G);
I_31 = -V1*c1*(Gs+G)-V2*c1*G-V3*c1*(Gs+2*G);
I_32 = -V1*c2*G-V2*c2*(Gs+G)+V3*c2*(Gs+2*G);
I_33 = -V1*c3*(Gs+G)-V2*c3*(Gs+G)+2*V3*c3*(Gs+G)+c1*c3*x1*(V3-V1)+c2*c3*x2*(V3-V2);
Itrans=[I_11 I_12 I_13;
I_21 I_22 I_23;
I_31 I_32 I_33];
G_total= c1*x1+c2*x2+c3*x3+3*Gs;
Additionalpart=((Gs^2+3*G*Gs)/G_total)*[2*V1-V2-V3;-V1+2*V2-V3;-V1-V2+2*V3];
I=(1/G_total)*Itrans*[x1;x2;x3]+Additionalpart;
I1=I(1);
I2=I(2);
I3=I(3);
I also have a question about unpacking the arry, If i write this code like this does it cause any problem?
[I1;
I2;
I3]= I;
I received error for this, How can I write a code in matalb in this way? to unpaching an array?
  1 commentaire
Matt J
Matt J le 30 Oct 2023
Modifié(e) : Matt J le 30 Oct 2023
I do not know why always the first element of the array I (which is I1=I(1) is Zero?
We don't know what "always" means. What inputs have you tried?

Connectez-vous pour commenter.

Réponses (3)

Torsten
Torsten le 30 Oct 2023
Déplacé(e) : Torsten le 30 Oct 2023
We don't know how you call the function - so we cannot predict I1, I2 and I3.
To your second question:
I = [3 5 -6];
[I1,I2,I3] = deal(I(1),I(2),I(3))
I1 = 3
I2 = 5
I3 = -6

Matt J
Matt J le 30 Oct 2023
Modifié(e) : Matt J le 30 Oct 2023
To unpack, you can do
[I1,I2,I3]=deal( I(1), I(2), I(3) )
or,
Icell=num2cell(I);
[I1,I2,I3]=Icell{:}

Walter Roberson
Walter Roberson le 30 Oct 2023
Modifié(e) : Walter Roberson le 30 Oct 2023
[I1;
I2;
I3]= I;
That code is valid only if I is a function (not function handle) that returns at least 3 outputs. It is not valid if I is a variable.
Unpacking a vector does not have any nice representation in MATLAB, unless you use a helper function or you use at least two lines of code.
%version 1
temp = num2cell(I);
[I1, I2, I3] = temp{:};
%version 2, more certainty, works in older MATLAB
temp = num2cell(I);
[I1, I2, I3] = deal(temp{:});
%version 3, one line but ugly
[I1, I2, I3] = struct('temp', num2cell(I)).temp;
%version 4, helper function
[I1, I2, I3] = unpack(I);
function varargout = unpack(value);
temp = num2cell(value);
[varargout{1:nargout}] = temp{:};
end
There is also a one-line version making use of subref but it is especially ugly.

Catégories

En savoir plus sur Aerospace Applications dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by