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

27 次查看(过去 30 天)
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