Struggling with equation and FOR loops

1 view (last 30 days)
Serena Solanki
Serena Solanki on 8 Nov 2017
Commented: Guillaume on 8 Nov 2017
Hi I am trying to code this equation into MATLAB, I have tried using for loops but have had no success
The code is as follows:
r=[1 2 3 4 5]
Ai=0.6895
%Ai+r=N
N=[0.3256 0.2254 0.1300 0.07684]
%delta =(1/r)*log(Ai/N)]
I want to solve the equation delta and find delta values for each of the Ai/N values. So I would like to achieve 1/1*log(Ai/0.3256), 1/2*log(Ai/0.2254) etc
Hope you can help

Answers (1)

Torsten
Torsten on 8 Nov 2017
Edited: Torsten on 8 Nov 2017
Ai=0.6895;
r=[1 2 3 4 5];
N=[0.3256 0.2254 0.1300 0.07684];
for i=1:numel(r)
for j = 1:numel(N)
delta(i,j)=1/r(i)*log(Ai/N(j));
end
end
Best wishes
Torsten.
  2 Comments
Serena Solanki
Serena Solanki on 8 Nov 2017
Hi Torsten
Thank you very much for this- it has worked perfectly :)
Guillaume
Guillaume on 8 Nov 2017
Well, if that's the desired result, and if using matlab R2016b or later, simply:
delta = 1./r'.*log(Ai./N);
And if using matlab < R2016b:
delta = bsxfun(@times, 1./r', log(Ai./N));
No need for loops in any case.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!