필터 지우기
필터 지우기

How to fill array with value 0 if function returns empty []

조회 수: 40 (최근 30일)
Sanders A.
Sanders A. 2022년 2월 21일
댓글: Sanders A. 2022년 2월 22일
I am creating a matrix for computing the natural frequency of beams with different boundary conditions. I have my assumed eigenfunction, I solve it at a boundary condition and then I want to collect all the terms attached to coefficients so I can collect thema ll and solve for the determinant which gives the characteristic equation to find the natural frequencies. However, the -C(T==c1)- terms return empty arrays which "disappear" when bringing them all together.
Is there an efficient way to replace these empty values with 0 as I show in the last line of my code?
I have tinkered around with cell arrays but they don't play nice with isempty without making loops.
syms x c1 c2 c3 c4 ki real
phi = c1*cos(ki*x) + c2*cosh(ki*x) + c3*sin(ki*x) + c4*sinh(ki*x);
phi_0 = simplify(subs( phi,x,0));
[C,T] = coeffs( phi_0,[c4 c3 c2 c1],'All'); %why is this backwards?
A = sym(zeros(4,4));
A(1,:) = [C(T==c1),C(T==c2),C(T==c3),C(T==c4)]; % Fails because RHS=[ki, ki] when it 'should' be [0, 0, ki, ki]
Unable to perform assignment because the size of the left side is 1-by-4 and the size of the right side is 1-by-2.

Error in sym/privsubsasgn (line 1229)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);

Error in sym/subsasgn (line 1060)
C = privsubsasgn(L,R,inds{:});

채택된 답변

DGM
DGM 2022년 2월 21일
편집: DGM 2022년 2월 21일
I hardly ever use symbolic tools, so I'm sure someone knows a better way. Just paying attention to the output alone, this is one way to do it with cell arrays:
syms x c1 c2 c3 c4 ki real
phi = c1*cos(ki*x) + c2*cosh(ki*x) + c3*sin(ki*x) + c4*sinh(ki*x);
phi_0 = simplify(subs( phi,x,0));
[C,T] = coeffs( phi_0,[c4 c3 c2 c1],'All'); %why is this backwards?
% using a cell array
A = cell(4,4);
A(1,:) = {C(T==c1),C(T==c2),C(T==c3),C(T==c4)}
A = 4×4 cell array
{[1 ]} {[1 ]} {0×1 sym } {0×1 sym } {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
% after whatever loop ostensibly fills the rest of A
mask = cellfun(@isempty,A) % find empties
mask = 4×4 logical array
0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
A(mask) = {sym(0)}; % replace with zero
A = reshape(vertcat(A{:}),4,4) % convert to sym array
A = 
  댓글 수: 1
Sanders A.
Sanders A. 2022년 2월 22일
That worked wonderfully! Thank you! Now my system is much more robust than it was before as I don't have to manually find out which terms are null and replace them with 0.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bob Thompson
Bob Thompson 2022년 2월 21일
I admit I don't entirely understand the math here, so there may be a more elegant solution. My MATLAB access is also currently down, so nothing here has been tested.
That said, you could just use some logical indexing to find the empty values and replace them ahead of time.
syms x c1 c2 c3 c4 ki real
phi = c1*cos(ki*x) + c2*cosh(ki*x) + c3*sin(ki*x) + c4*sinh(ki*x);
phi_0 = simplify(subs( phi,x,0));
[C,T] = coeffs( phi_0,[c4 c3 c2 c1],'All'); %why is this backwards?
A = sym(zeros(4,4));
C(isempty(C)&T==c1) = 0; % Replace empty values with 0
A(1,:) = [C(T==c1),C(T==c2),C(T==c3),C(T==c4)]; % Fails because RHS=[ki, ki] when it 'should' be [0, 0, ki, ki]
  댓글 수: 1
Sanders A.
Sanders A. 2022년 2월 22일
I am not sure what your second to last line is doing and it does not seem to have quite the desired effect as C is unchanged by the action of that line. The last line continues to fail for the same reason as before RHS is a 1x2.
I truly appreciate the suggestion though!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by