Returning Multiple Variables from a function to be added to existing variable.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Jan
2022-12-29
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 个评论
Jan
2022-12-30
If only the first output of the function matters, this is a valid Matlab syntax:
k = k + element_stiffness()
Image Analyst
2022-12-29
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 个评论
Image Analyst
2022-12-30
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!