I see equivalent operations being done several times in my Matlab code, and I think, there must be a way to condense these and do it all once. In this case it was checking whether some variables were less than a certain epsilon:
if abs(beta) < tol
beta = 0;
end
if abs(alpha) < tol
alpha = 0;
end
if abs(gamma) < tol
gamma = 0;
end
I tried combining these into a few lines that deal with the whole boodle,
overTol = [alpha,beta,gamma] >= tol;
[alpha,beta,gamma] = [alpha,beta,gamma] .* overTol;
If this was Python, where a list is a list is a list, that would be fine, but in Matlab, an operation on the right produces one variable on the left except in special situations.
After reading Define multiple variables at the same time in MATLAB? and Declare & initialize variables in one line in MATLAB without using an array or vector, I tried using deal
, but [alpha,beta,gamma] = deal([alpha,beta,gamma] .* overTol);
does not distribute the terms of the vector given to the deal function between the terms in the vector on the left, but instead gives a copy of the whole vector to each of the terms.
The last thing I want to do is set the right equal to a unique vector and then set alpha, beta, and gamma equal to the terms of that vector, one by one. That's just as ugly as what I started with.
Is there an elegant solution to this?