Returning Multiple Variables from a function to be added to existing variable.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a function below that returns multiple variables/matrices seen below. Rather than returning to the variable K_1 i would like to add its contents to an existing variable K. I am unsure how to do this when returning multiple variables and still want to return to the other variables.
[k_1,K_1,B1,b1,c1]=element_stiffness([2,4,1],Coords, v,E,t,D)
0 commentaires
Réponses (2)
Jan
le 29 Déc 2022
What about:
[k_1,K_1,B1,b1,c1]=element_stiffness([2,4,1],Coords, v,E,t,D)
k = [k, k_1];
% Or:
k = k + k_1;
% Or:
k = zeros(5, 100); % Pre-allocate
for i = 1:5
[k(i, :), K_1,B1,b1,c1]=element_stiffness([2,4,1],Coords, v,E,t,D)
end
2 commentaires
Jan
le 30 Déc 2022
If only the first output of the function matters, this is a valid Matlab syntax:
k = k + element_stiffness()
Image Analyst
le 29 Déc 2022
What about
[K(end+1), K_1, B1, b1, c1] = element_stiffness([2,4,1],Coords, v,E,t,D)
This puts the return variable into an element at the end of the K vector. An additional element is created. So if K was [213, 4234] then after calling this if k_1 was 999, the new K would be [213, 4234, 999].
2 commentaires
Image Analyst
le 30 Déc 2022
Sure, if K_1 and K are 2-D 10x10 matrices, then you can certainly return a temporary variable K_1, and then add that to K like you did.
But you have to do it in MATLAB syntax
K = K + Temp_variable;
not C syntax
K += Temp_variable
The size and shape of Temp_variable has to match that of K of course.
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!