Multiplying By 10000 vs 10^4

10 vues (au cours des 30 derniers jours)
Harel Harel Shattenstein
Harel Harel Shattenstein le 27 Avr 2018
Modifié(e) : Stephan le 27 Avr 2018
Why
A=rand(3,3);
B=A(:)*10000;
is different from
A=rand(3,3);
B=A(:)*10^4;
  2 commentaires
Stephan
Stephan le 27 Avr 2018
Hi,
it isnt:
>> A = rand(3,3)
A =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
>> B= A(:)*10000
B =
1.0e+03 *
8.1472
9.0579
1.2699
9.1338
6.3236
0.9754
2.7850
5.4688
9.5751
>> C = A(:)*10^4
C =
1.0e+03 *
8.1472
9.0579
1.2699
9.1338
6.3236
0.9754
2.7850
5.4688
9.5751
>> Yes_it_is_the_same = B == C
Yes_it_is_the_same =
9×1 logical array
1
1
1
1
1
1
1
1
1
Stephan
Stephan le 27 Avr 2018
Modifié(e) : Stephan le 27 Avr 2018
the way you wrote it, will generate a new matrix A with new random numbers, which will most likely not be the same as in the first run:
>> clear all
>> A = rand(3,3)
A =
0.9649 0.9572 0.1419
0.1576 0.4854 0.4218
0.9706 0.8003 0.9157
>> B= A(:)*10000
B =
1.0e+03 *
9.6489
1.5761
9.7059
9.5717
4.8538
8.0028
1.4189
4.2176
9.1574
>> A = rand(3,3)
A =
0.7922 0.0357 0.6787
0.9595 0.8491 0.7577
0.6557 0.9340 0.7431
>> C = A(:)*10^4
C =
1.0e+03 *
7.9221
9.5949
6.5574
0.3571
8.4913
9.3399
6.7874
7.5774
7.4313
>> Yes_it_is_the_same = B == C
Yes_it_is_the_same =
9×1 logical array
0
0
0
0
0
0
0
0
0
So you understand the difference between both versions?
bets regards

Connectez-vous pour commenter.

Réponses (1)

Stephan
Stephan le 27 Avr 2018
See comments, when working with random numbers you have to understand, that everytime you use the command new random numbers will appear - thats the reason why you get different values for the supposed same thing.
Best regards
Stephan

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by