Using vpasolve to solve equation for all values in matrix
7 次查看(过去 30 天)
显示 更早的评论
Is it possible to use vpasolve to solve a particular equation for all values in a matrix without looping? i have a large number of matrices and looping through them all will be very slow.
% Minimal working example
h=rand(5,5);
h_long=reshape(h,[size(h,1)*size(h,2),1]);
store_arr=zeros(numel(h_long),1);
for a=1:numel(h_long)
curr_pix=h_long(a);
tt=vpasolve(5*((1-exp(-5/x)*sind(14))/((1-exp(-5/x))*cosd(14)))==curr_pix, x);
store_arr(a)=tt;
end
fin_mat=reshape(squeeze(store_arr),size(h))
This is a miniature version of what i am doing. my matrix has 4 dimensions and are quite large.
Thanks,
Maria
0 个评论
采纳的回答
Walter Roberson
2018-6-4
No, vpasolve() will always consider matrix inputs as being equations to be solved simultaneously.
If you switch to symbolic curr_pix then you can solve() the equation, getting
ttsol = -5./ln(-(-5+curr_pix*cosd(14))./(-curr_pix*cosd(14)+5*sind(14)))
after which you can fin_mat = subs(ttsol, curr_pix, h) to get a matrix of solutions that skips the reshapes()
3 个评论
Walter Roberson
2018-6-4
As long as you have the values of h, then what I showed should work.
h = ... whatever
syms curr_pix x
angd = sym(14);
ttsol = simplify( solve(5*((1-exp(-5/x)*sind(angd))/((1-exp(-5/x))*cosd(angd)))==curr_pix, x) );
fin_mat = subs(ttsol, curr_pix, h);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!