Assign value to column in matlab - matlab

In matlab if
a = zeros(1,2); b= ones(2,1) then it is wrong a+b;
but X=zeros(2,2), X(:,1)=a works well, why?

Good question. The exact answer can be found on the Matlab site, right near the bottom of the page:
Indexing on Assignment When assigning values from one matrix to
another matrix, you can use any of the styles of indexing covered in
this section. Matrix assignment statements also have the following
requirement.
In the assignment A(J,K,...) = B(M,N,…), subscripts J, K, M, N, etc.
may be scalar, vector, or array, provided that all of the following
are true:
The number of subscripts specified for B, not including trailing
subscripts equal to 1, does not exceed ndims(B).
The number of
nonscalar subscripts specified for A equals the number of nonscalar
subscripts specified for B. For example, A(5, 1:4, 1, 2) = B(5:8) is
valid because both sides of the equation use one nonscalar subscript.
The order and length of all nonscalar subscripts specified for A
matches the order and length of nonscalar subscripts specified for B.
For example, A(1:4, 3, 3:9) = B(5:8, 1:7) is valid because both sides
of the equation (ignoring the one scalar subscript 3) use a 4-element
subscript followed by a 7-element subscript.
When you look at your example, it follows the last point in the above: although you are assigning to X(:,2) which is a 2x1 column vector, and the right hand side is a 1x2 row vector, the rule "order and length of all non scalar subscripts specified for A matches the order and length of non scalar subscripts specified for B".
Note that the same is not true when you want to add two matrices - in that case, they actually need to be of exactly the same shape (so you can't add a 2x1 and a 1x2 matrix, even though both have two elements).

With "X(:,1)=b", you are asking MATLAB to copy the values of the column vector b to the first column of X. b has two rows, and so does X. So the value assignment works.
As for "a+b", matrix-matrix addition requires exact match of dimensions.

Related

What is the colon operator doing here?

What are these lines of code doing?
x0 = rand(n,2)
x0(:,1)=W*x0(:,1)
x0(:,2)=H*x0(:,2)
x0=x0(:)
Is this just one big column vector?
I'd encourage you to take a MATLAB Tutorial as indexing arrays is a fundamental skill. Also see Basic Concepts in MATLAB. Line-by-line descriptions are below to get you started.
What are these lines of code doing?
Let's take this line by line.
1. This line uses rand() to generate an n x 2 matrix of uniform random numbers (~U(0,1)).
x0 = rand(n,2) % Generate nx2 matrix of U(0,1) random numbers
2. Multiply the first column by W
In this case, x0(:,1) means take all rows of x0 (the colon in the first argument) and the 1st column (the 1). Here, the * operator indicates W is a scalar or an appropriately sized array for feasible matrix multiplication (my guess is a scalar). The notation .* can be used for element-by-element multiplication; see here and here for more details.
x0(:,1)=W*x0(:,1) % Multiply (all rows) 1st column by W
3. Multiply the first column by H.
Using similar logic as #2.
x0(:,2)=H*x0(:,2) % Multiply (all rows) 2nd column by H
4. Force column
The x0(:) takes the array x0 and forces all elements into a single column.
From the documentation for colon:
A(:) reshapes all elements of A into a single column vector. This has
no effect if A is already a column vector.
A related operation is forcing a row vector by combining this with the transpose operator.
For example, try the following: x0(:).'
x0 = x0(:) % Force Column
x0 = x0(:).' % Force Row
Related Posts:
What is Matlab's colon operator called?
How does MATLAB's colon operator work?
Combination of colon-operations in MATLAB

Determine the 'greatest' singular vector of U matrix after SVD in Matlab

It is known that in Matlab SVD function outputs three matrices: [U,S,V] = svd(X).
Actually, 'U' is a square m X m matrix where m is the number of rows/columns. Also, 'S' is a non-square matrix with dimensions m X n that stores n singular values (produced from left singular vectors of U matrix) in descending order(in diagonal).
My question is how to determine (in Matlab) which 'm' singular vectors of matrix 'U' correspond to the first (greatest) singular value of the 'S' matrix. Furthermore, some values of the specific singular vector are positive and others are negative. Does this minus or plus sign hides any mathematical meaning? I have seen examples that use the sign of the 'greatest' singular vector as for classification purposes.
The diagonal of the S matrix contains the singular values. So for the ith singular value (in the i,i position on S matrix), ith column of the U and V vectors respectively for the two constraint equations.
I don't think the +/- hides any special meaning. After all, you could multiply both the U and the V matrices by a -1 constant and the result would still be valid.
To be perfectly accurate, by definition singular values of SVD are not necessarly reordered, but MATLAB SVD reorders them.
The ith column of U corresponds to the ith singular value of M.
Namely for the ith singular value sigma_j, there exist j such that
M* .u_i = sigma_j v_j
you also have
M. v_j = sigma_i u_i
Be careful, it might not be what you are looking for.
The coordinates of your singular values are the coordonates in the original basis. A positive values means your new variable is positively proportional to the corresponding original variable. In statistics it is generally used when you know that both original and transformed variables increase or decrease together.

Dot product of two 3 x 11 matrices in matlab is giving a 1x11 matrix

I am doing the dot product of two 3x11 matrices in MATLAB, these matrices are multidimensional but I am only accessing one 'page' at a time. From my maths knowledge I would expect a 3x11 matrix back as result but when using the following code:
contact_force(:,:,k)=dot(km(:,:,k),actual_interf(:,:,k))
The answer is in the form of a 1x11 matrix
For N-D arrays, dot computes the scalar product along the first non-singleton dimension. So for two 3x11 arrays, it computes 11 dot products returning scalars, hence a 1x11.
This is the equivalent of:
sum(A.*B)
If you do not intend to perform the dot product, omit the sum to just get element-wise multiplication.
The dot product of two vectors multiplies each element together, then sums the result to produce one number.
When the dot() function in Matlab is called on two 2D matrices, the dot product is computed for each set of columns. Thus, for each column you get a scalar result, which is why the answer is returned as 1x11
C = dot(A,B) returns thescalar product of the vectors A and B.
if your matrices are 2D,eg. A and B are m*n matrices, then it will calculate dot product of each columns of A and B. and as you know dot product of two vectors is a single number. thus it will give you an 1 * n vector as resutl;

MATLAB: why transpose single dimension array

I have the following Matlab code snippet that I'm having to translate to VBScript. However, I'm not understanding why the last line is even necessary.
clear i
for i = 1:numb_days
doy(i) = floor(dt_daily(i) - datenum(2012,12,31,0,0,0));
end
doy = doy';
Looking over the rest of the code, this happens in a lot of other places where there are single dimension arrays (?) being transposed in place. I'm a newbie when it comes to both these languages, as well as posting a question on Stack, as I'm a sleuth when it comes to finding answers, just not in this case. Thanks in advance.
All "arrays" in MATLAB have at least two dimensions, and can be treated as having any number of dimensions you wish. The transpose operator here is converting between a row (size [1 N] array) and a column (size [N 1] array). This can be significant when it comes to either concatenating the arrays, or performing other operations.
Conceptually, the dimension vector of a MATLAB array has as many trailing 1s as is required to perform an operation. This means that you can index any MATLAB array with any number of subscripts, providing you don't exceed the bounds, like so:
x = magic(4); % 4-by-4 square matrix
x(2,3,1,1,1) % pick an element
One final note: the ' operator is the complex-conjugate transpose CTRANSPOSE. The .' operator is the ordinary TRANSPOSE operator.

MATLAB matrix replacement assignment gives error

I tried to update some part of a matrix, I got the following error message:
??? Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts
My code tries to update some values of a matrix that represent a binary image. My code is as follows:
outImage(3:5,2:4,1) = max(imBinary(3:5,2:4,1));
When I delete last parameter (1), this time I get the same error. I guess there is a mismatch between dimensions but I could not get it. outImage is a new object that is created at that time (I tried to create it before, but nothing changed). What may be wrong?
You mention in one of your comments on another answer that you are trying to create your own dilation algorithm, and therefore want to take the maximum value in a 3-by-3-by-1 submatrix and replace the values in that submatrix with the maximum value. The function MAX will by default operate along the columns of your submatrix, which will give you a 1-by-3 matrix (i.e. the maximum values of the columns of your 3-by-3-by-1 matrix). The error results because MATLAB can't assign a 1-by-3 matrix to a 3-by-3-by-1 matrix.
One solution is to call MAX again on your 1-by-3 matrix to get a scalar value, which you can then assign to each element of your 3-by-3-by-1 submatrix without error:
outImage(3:5,2:4,1) = max(max(imBinary(3:5,2:4,1)));
On the rhs of your equation you take the max of a 3x3x1 sub-matrix, which returns a 1x3 vector. You then try to assign this to a 3x3x1 sub-matrix. A singleton subscript is one with the value 1. So the rhs has 1 non-singleton subscript, and the lhs has 2. Matlab can't figure out how to expand a 1x3 matrix to fill a 3x3x1 space.
I'm not entirely sure what you want to do, so I won't guess a solution. Do you want to make 3 copies of the rhs and put one into each row of the sub-matrix on the lhs ? Or are you trying to construct a 3x3x1 matrix on the rhs ?
Do you want to fill all indexed elements in outImage by maximum value for each column of rhs expression? You can expand the row you get on rhs with REPMAT:
outImage(3:5,2:4,1) = repmat(max(imBinary(3:5,2:4,1)),3,1)
outImage(3:5,2:4) works as well.
I got the same error before, and what I have done was defining the left hand matrix before. I don't know if you have the same case but you can try the following:
outImage=Zeros(M,N,K);
M, N, and K are the dimensions that you have. Then just type:
outImage(3:5,2:4,1) = max(max(imBinary(3:5,2:4,1)));