all function B = all(A < 0.5,3)

4 vues (au cours des 30 derniers jours)
Marie Nancy B
Marie Nancy B le 14 Juin 2023
Modifié(e) : Stephen23 le 15 Juin 2023
This is straight forward to me:
A = [0.53 0.67 0.01 0.38 0.07 0.42 0.69]
A = 1×7
0.5300 0.6700 0.0100 0.3800 0.0700 0.4200 0.6900
B = all(A < 0.5,1)
B = 1×7 logical array
0 0 1 1 1 1 0
this also makes sense -
A = [0.53 0.67 0.01; 0.38 0.07 0.42 ]
A = 2×3
0.5300 0.6700 0.0100 0.3800 0.0700 0.4200
B = all(A < 0.5,2)
B = 2×1 logical array
0 1
But why does this yield the same result?
A = [0.53 0.67 0.01 1.1; 0.38 0.07 0.42 0.01]
A = 2×4
0.5300 0.6700 0.0100 1.1000 0.3800 0.0700 0.4200 0.0100
B = all(A < 0.5,3)
B = 2×4 logical array
0 0 1 0 1 1 1 1
OR
B = all(A < 0.5,4)
B = 2×4 logical array
0 0 1 0 1 1 1 1
  1 commentaire
Stephen23
Stephen23 le 15 Juin 2023
Modifié(e) : Stephen23 le 15 Juin 2023
"But why does this yield the same result?"
Consider the sub-arrays (i.e. vectors) along each of the specified dimensions: what values do they have in them? Remember that all arrays implicitly have infnite trailing singleton dimensions: the fact that each of those vectors for dimensions 3 and 4 happen to have one element in them is irrelevent, ALL applies its algorithm on that one element. It might help to revise this: https://www.mathworks.com/help/matlab/math/multidimensional-arrays.html
Lets consider the top left corner of your data:
A = [0.53 0.67 0.01 0.38 0.07 0.42 0.69];
A(1,1,:) % top left, all elements along 3rd dimension
ans = 0.5300
A(1,1,1,:) % top left page1, all elements along 4th dimension
ans = 0.5300
You can repeat this yourself for every other element in your matrix.
Question: what do you expect the result to be? Why?

Connectez-vous pour commenter.

Réponses (2)

the cyclist
the cyclist le 14 Juin 2023
Modifié(e) : the cyclist le 14 Juin 2023
The reason is that all MATLAB arrays have implied singleton dimension beyond the 2nd dimension, if they have not been actually specified.
A = [0.53 0.67 0.01 1.1;
0.38 0.07 0.42 0.01];
size(A,47)
ans = 1

Torsten
Torsten le 14 Juin 2023
Déplacé(e) : Torsten le 14 Juin 2023
But why does this yield the same result?
Because A has only 2 dimensions, not 3 or 4. Thus "all" operates on all elements separately in both cases.
  1 commentaire
VBBV
VBBV le 15 Juin 2023
Thus "all" operates on all elements separately in both cases., Yes, thats correct. But if one uses 'all' option it would do only once.
A = [0.53 0.67 0.01 1.1; 0.38 0.07 0.42 0.01]
A = 2×4
0.5300 0.6700 0.0100 1.1000 0.3800 0.0700 0.4200 0.0100
B = all(A < 0.5,'all')
B = logical
0
B = all(A < 0.5,3)
B = 2×4 logical array
0 0 1 0 1 1 1 1
B = all(A < 0.5,4)
B = 2×4 logical array
0 0 1 0 1 1 1 1

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by