I have the following problem in MATLAB.
d is a dense matrix dimensions c,t
f is a dense matrix dimensions u,p
p is a cell array (dimension p) in which each cell contains a logical sparse matrix of indexes of dimensions c,t
I want to execute the following instruction:
for r=1:u
f(r,:) = mean(mean(d(p{:})))
end
I noted that if p contains full logical matrices the command works, but since they are sparse it isn't working. It gives me the error: "matrix dimensions must agree".
I know that the : syntax concatenates the content of an array, and I know that I can circumvent it with another for cycle.
Does anyone knows how to execute this command without an additional for cycle?
Related
I am using SIMULINK and I needed to define a Rotation Matrix 3,3,N where N is the number of Robots which I am trying to simulate. To do that, because I am also using the Simulink Coder I had to define the signal related to this matrix as Variable Size and I had to define the upper-bound in the following way:
The problem is that when I want to use only one robot (I set n_robots to 1) I get the following error.
Cannot initialize dimensions of 'R' of 'test_pos_ctrl_target/rotation matrix to Euler angles' to [3x3x1]. When the number of dimensions of a matrix exceeds 2, the size of the trailing dimension must be greater than 1.
Someone could help me?
thanks a lot.
You can't have the last dimension as 1 because MATLAB treats any matrix of dimension [m,n,1] as [m,n]. See size() returns 1 where matrix dimension should not exist for more details.
Try defining R of size [n_robots,3,3] and then re-arrange the matrix inside your code (I assume you are using a MATLAB Function block).
In my current analysis, I am trying to multiply a matrix (flm), of dimension nxm, with the inverse of a matrix nxmxp, and then use this result to multiply it by the inverse of the matrix (flm).
I was trying using the following code:
flm = repmat(Data.fm.flm(chan,:),[1 1 morder]); %chan -> is a vector 1by3
A = (flm(:,:,:)/A_inv(:,:,:))/flm(:,:,:);
However. due to the problem of dimensions, I am getting the following error message:
Error using ==> mrdivide
Inputs must be 2-D, or at least one
input must be scalar.
To compute elementwise RDIVIDE, use
RDIVIDE (./) instead.
I have no idea on how to proceed without using a for loop, so anyone as any suggestion?
I think you are looking for a way to conveniently multiply matrices when one is of higher dimensionality than the other. In that case you can use bxsfun to automatically 'expand' the smaller matrix.
x = rand(3,4);
y = rand(3,4,5);
bsxfun(#times,x,y)
It is quite simple, and very efficient.
Make sure to check out doc bsxfun for more examples.
I am using Matlab R2011a. I have a matrix A which is of size 27*3355432. This matrix has a lot of zeros. I need to compute the comand eig(A'*A) but I can't even do the A'*A. I have tried using the sparse matrix B = sparse(A)and then computing B'*B but I get the error:
??? Error using ==> mtimes
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead
Truth is I am not a Matlab expert. Is there a way to produce such a database?
I have a sparse matrix S.
I perform the following operation
D1 = diag(sum(S,2)), basically forming a diagonal matrix.
Now I need to perform (D1)^(-0.5), but I get an error
"Error using mpower, use full(x)^full(y)"
Converting to full will defeat the purpose of using a sparse matrix.
Any advice will be very helpful.
Raising a diagonal matrix to a power can be done simply by doing the operation on the diagonal elements elementwise... so:
D1_diagonal_elements = sum(S,2);
your_result = diag(D1_diagonal_elements .^ (-0.5));
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)));