Using vpasolve to solve equation for all values in matrix

12 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jun 2018
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 Comments
Walter Roberson
Walter Roberson on 4 Jun 2018
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);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!