"Error using *". Help with my Monte Carlo Simulation
显示 更早的评论
I have created code on low rank assumption using nuclear norm using CVX. I'm thinking this error code is being caused by the variable X not being recognised but im not sure. Here is the following error code -
Error using *
Incorrect dimensions for matrix multiplication. Check that the
number of columns in the first matrix matches the number of rows
in the second matrix. To perform elementwise multiplication, use
'.*'.
Error in Untitled34 (line 36)
y(i_p) = trace(M_p{i_p}*X);
Related documentation
Here is my code :
clear all;
clc;
MONTE_CARLO = 10;
n = 10;
m = 20;
k = 5;
U = randn(m,k);
V = randn(k,n);
X = U*V
for p = 1 : 50 : n^2 % Number of measurements
for mc = 1 : MONTE_CARLO % Monte Carlo simulations
M_p = cell(p, 1); % Cell with measurement matrices
y = zeros(p, 1); % Vector of measurements
for i_p = 1 : p % Generate measurement matrices
M_p{i_p} = randn(n, n);
y(i_p) = trace(M_p{i_p}*X);
end
cvx_begin
variable Xe(n,n);
minimize(norm_nuc(Xe));
subject to
for i_p = 1 : p
trace(Xe*M_p{i_p}) == y(i_p);
end
cvx_end
end
end
8 个评论
Jan
2021-1-25
Do you see, that your code is hard to read? Please format it as code and remove the pile of blank lines.
Karim Wahdan
2021-1-25
Bob Thompson
2021-1-25
'Please format it as code'
Jan is referring to the option to 'Insert a line of code' in the posting section. You can also select the block of code and press Alt+Enter.
It looks like X is defined to be a 20 x 10 size matrix, but M_p{i_p} is defined as a 10 x 10 matrix. You can multiply X*M_p{i_p}, but not the other way around.
Perhaps you want the transpose of X?
Karim Wahdan
2021-1-25
Bob Thompson
2021-1-25
Transposing a matrix is simple, just add an apostrophe after it.
X_Transpose = X';
You can also just include this in a greater equation
y(i_p) = trace(M_p{i_p}*X');
Bob Thompson
2021-1-25
Now that I think about it, transposing X isn't going to give you a square matrix anyway. X is not a square matrix, but M_p{i_p} is, so no matter how you multiply them together they will not result in a square matrix output.
Karim Wahdan
2021-1-27
Bob Thompson
2021-1-27
I don't know, because I don't know your problem statement to understand what X is supposed to be, or why you need a square matrix. It seems to me that this is not a MATLAB specific issue, and I recommend taking another look at the purpose for your code, rather than the specifics of the code itself. Sorry I can't be more help.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!