Clear Filters
Clear Filters

i have different result

3 views (last 30 days)
Hichem Younsi
Hichem Younsi on 18 Feb 2020
Answered: Steven Lord on 18 Feb 2020
i have a simple question, i don't knew if it setting probleme or not
i'm makin a loop to calculate some variables.
for T=[100;1000;10000];
so i get ProbDG= [1 1 0.9999] instead of [0.9900 0.9990 0.9999] and DGT= [ Inf Inf 9.2103] instead of [ 9.21 6.91 4.60 ]
i don't understand why i have this number because in Ecxel i get the right result

Accepted Answer

Adam on 18 Feb 2020
Edited: Adam on 18 Feb 2020
Just get rid of the loop and vectorise:
It can be done in a loop, but it's the end of the day and I'm too tired to work out exactly what is wrong with yours other than that you aren't saving any intermediate results each iteration and keep overwriting them! It's a lot more efficient vectorised anyway.

More Answers (1)

Steven Lord
Steven Lord on 18 Feb 2020
for T=[100;1000;10000]; % Original code
When you call for with a vector as its loop variable definition, it iterates over the columns of the matrix you specify (not the elements.) So T does not take on the elements of [100; 1000; 10000] in turn, instead the loop iterates once with the whole column vector as T.
ProbDG=1-(1/T); % Original code
What does the / operator do when given a matrix and a vector as input?
"x = B/A solves the system of linear equations x*A = B for x."
So ProbDG is a vector such that x*[100; 1000; 10000] = 1.
T = [100; 1000; 10000]; % New code
x = 1/T;
x is not unique, three possible vectors x could be are [1/100, 0, 0], [0, 1/1000, 0], or [0, 0, 1/10000].
Since you wanted to compute the reciprocal of each element of T and subtract that result from 1, you need to use the ./ operator instead.
DGT=-log(-log(ProbDG)) % Original code
Because ProbDG is not what you expected, DGT will not be either.
You could receive the results you expected by either making the T vector a row vector or using the ./ operator in the definition of ProbDG. If you make T a row vector the loop body will run three times, each on a scalar value. For scalar T the expressions 1/T and 1./T work the same way. Or you could just eliminate the loop and use the ./ operator as Adam suggested.


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!