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!