# i have different result

7 Ansichten (letzte 30 Tage)
Hichem Younsi am 18 Feb. 2020
Beantwortet: Steven Lord am 18 Feb. 2020
hello;
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];
ProbDG=1-(1/T);
DGT=-log(-log(ProbDG))
end
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
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Bearbeitet: Adam am 18 Feb. 2020
Just get rid of the loop and vectorise:
ProbDG=1-(1./T);
DGT=-log(-log(ProbDG));
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.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Steven Lord am 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*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
end
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.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by