what is difference between C=+A, C=A, C=unary(A)? All three provide same assignment of matrix A to C

I cant understand the difference between C=+A, C=A, C=unary(A)?
All three provide same assignment of matrix A to C. Is there any difference among them

 Risposta accettata

C=+A;
C = uplus(A) is an alternate way to execute +A, but is rarely umeased. It enables operator overloading for classes.
So depending on class of A, +A or uplus(A) may have different meaning.
C = A; This is just an assignment.
[Update based on comments below]
For assignment:
  • C = A; This is assiment only. MATLAB will do a lazy copying (pointer to A) and only do the real data copying when C is changed later.
  • C = +A or C=uplus(A); +A and uplus(A) is the expression or function that involves A. The meaning of + and uplus is class dependent. The expression and function need to be evaluated first before assignment. The evaluation results is assigned to C (data copying is necessary).
The meaning of +/upluse:
  • For numerical data types like double/single/etc, this will have no effect of the results.
  • For logical and char, +/upluse has the effect of operator overloading. The result is of numerical data type double.
As function input arguments:
  • +A and uplus(A) is the expression, as in table(+A)
In addition, it is possible (but rarely) for user to do the operater overloading of +/uplus.

5 Commenti

For nearly all classes,
C = +A
differs from
C = A
in that +A is an expression rather than a direct reference to a variable. For example
table(A)
is going to generate a table with variable name A, but
table(+A)
is going to say that +A is a calculation rather than a variable and so will use default variable name, typically var1
inputname() would be empty for +A but not for A by itself.
There are some uncommon reasons why sometimes it is important to generate a copy of something rather than the original, such as wanting to prevent in-place optimization
In theory, someone could choose to implement a class in which the unary + operator (formal name uplus) did something other than return (a copy of) the data. I have never seen anyone do that. I could imagine someone might choose to define unary + and unary - for quaternions to refer to twists or something like that, so I would not say it will never happen.
x = true
x = logical
1
y = +x
y = 1
whos x y
Name Size Bytes Class Attributes x 1x1 1 logical y 1x1 8 double
That's the only example that comes to mind offhand among built-in types. If I recall correctly this is because sparse used to be (16ish years ago) a class rather than an attribute and logical used to be an attribute that could be applied to either double or sparse arrays rather than a class. In order to keep arithmetic operating on logical arrays like it did on double (logical) arrays true is treated as 1 and false as 0.
That's true, I'd forgotten about char. I guess I was a little more tired than I thought when I wrote that response.

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Prodotti

Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by