Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between first element of 1x1 matrix and all elements of 1x1 matrix

Tags:

matrix

matlab

After finding some logic in how empty structs are dealt with, I wanted to check how this generalized to matrices.

Here I noticed the following:

If you have a 1x1 matrix, and assign to the first element. It is not the same as assigning to all elements.

This rather surprised me as the first element is really the same as all the elements in this case. Here are my observations:

x = 1;
y = 1;
z = 1;

x(:) = []; % Evaluates to [] as I expected
y(1) = []; % Evaluates to Empty matrix: 1-by-0, rather than []
z(1,1) = []; %Throws an error: 'Subscripted assignment dimension mismatch.' even though size(z)  gives [1 1];
z(1,:) = []; % Evaluates to Empty matrix: 0-by-1, just like z(:,:) = []

After seeing this my question is:

Why does assigning to the same thing in different ways, lead to four different outcomes?

like image 289
Dennis Jaheruddin Avatar asked Sep 23 '13 12:09

Dennis Jaheruddin


People also ask

Is there 1x1 matrix?

A 1x1 matrix is a scalar. A null matrix has 0 for all of its entries. If the number of rows of a matrix is the same as the number of its columns, then it is a square matrix.

Does a 1x1 matrix have an inverse?

The inverse of a 1x1 matrix is simply the reciprical of the single entry in the matrix; eg. [5]-1 = [1/5] and [5]•[1/5] = [1]. The inverse of a 2x2 matrix can be found by using the following formula: Since division by zero is not allowed, the determinant of the matrix cannot be zero.


1 Answers

It just seems like a consistency thing.

Lets consider a bigger matrix and see if the behaviour is consistent with a 1-by-1 matrix (spoiler alert, it is in my opinion):

X = rand(3);

Case 1:

X(1,1) = []

It would make no sense for this to work. We can't preserve the shape and drop a single element hence we get a dimension mismatch error, which is consistent with your observation. Also dimension mismatch is an appropriate error as we're trying to force a 0-by-0 matrix into a 1-by-1 slot. (BTW on a side note you say size(z) gives you [1 1] but size(z, 3) also gives you 1 and so does size(z,7) etc so actually it's a [1 1 1 ... matrix)

Case 2:

X(1) = []

This results in an X such that size(X) is 1-by-8, MATLAB seems happy to linearize your matrix if you specify a linear index. This makes sense to me, and again is consistent with the 1-by-1 case since it results in a 1-by-numel(X)-1 matrix (i.e. 1-by-0 for X = 1)

Case 3:

X(1,:) = []

That's pretty straight forward, remove the first row so now we have an n-1-by-m matrix. So a 3-by-3 becomes a 2-by-3 so I'm happy with a 1-by-1 becoming a 0-by-1 in this case. Note that X(:,1) = [] is also consistent in a similar vein.

Case 4:

X(:) = []

This one just makes sense, re-assign the entire matrix. No issues. No ambiguity.

So even though sure, they all could unambiguously mean the same thing. I think it's a perfectly reasonable design choice for MATLAB to have different results that are consistent with larger matrices than always do the same thing for a single element matrix.

like image 62
Dan Avatar answered Sep 20 '22 13:09

Dan