How to edit a 3D matrix least square problem with for circulation;N-dimensional indexing allowed for Full matrices only

I want to solve a matrix least square problem, the solution is a 44 matrix. A 44matrix multiply a 41 matrix obtain a 41 matrix. If I repeat the process, it can be represented as a 44matrix multiply a 4N matrix obtain a 4*N matrix, I want to solve the problem in Matlab using for circulation, but the wrong code(N-dimensional indexing allowed for Full matrices only) appeared after running the program.
first form:
for i=1:3
cvx_begin
variable muler(4,4);
minimize(norm(muler(4,4)*xx-double(sp3(:,:,i))));
cvx_end
mu(:,:,i)=muler(4,4);
end

second form:
for i=1:3
sp4=double(sp3(:,:,i));
cvx_begin
variable muler(4,4);
minimize(norm(muler(4,4)*xx-sp4));
cvx_end
mu(:,:,i)=muler(4,4);
end
Both of the code is wrong, how to solve the problem? I use i=3 for simple calculation.

Show us the output of ’whos' from just before the minimize statement, so that we know what the dimensions are everything are. Here is a hint: if instead of declaring mule as a CVX variable, you just make up a numerical matrix of the same dimensions, such as eye(4), the expression inside the minimize() better be conformal, i.e., should evaluate in MAT:AB without producing an error message. Get that correct first. Then go back to declaring mule as a CVX variable.

is it your intention that the expression being minimized is a vector? If so, then norm is what you likely want. However, if the result is supposed to be a (non-vector) matrix, then perhaps you want norm(…,‘fro’)/.

Thank you!

This is a right code:

sp5=[28788,36006,42048,46365,48284,48175;
3768,5761,10542,16867,24186,31492;
28353,21313,13689,8001,4134,3749;
47564,46624,43351,38710,32395,24765];

xx=[49790,49790,49790,49790,49790,49790;
49790,46787.2955889304,38141.3528228939,24895.0000000000,8645.94276603647,-8645.94276603646;
0,17029.1829361850,32004.3950862928,43119.4048544272,49033.5780224778,49033.5780224778;
0,0,0,0,0,0];
cvx_begin
variable muler(4,4);
minimize(norm(muler*xx-sp5));
cvx_end

The out put is named as muler, it is a 44 matrix, when we have many other sp5, then sp5 changed to a 46N matrix, xx remains the same, a 46 matrix, the output should be N number of 4*4 matrix. I want to realize the process by a circulation rather than calculate the muler one by one, then there is a problem that I can’t realize it.
I want to use only three sp5 to calibrate the code.
The other two sp5 are:
sp5=[29131 36241 42214 46221 47758 48472;
3770 5608 10524 16486 24328 31739;
28194 20933 13914 7758 4132 3847;
46757 46275 43207 38334 32333 24727];

sp5=[28774 36492 42283 45925 47956 47889;
3727 5551 10458 16821 24298 31181;
28234 20971 13511 7949 4094 3786;
46605 45726 42739 38324 31713 24225 ];

It sounds like you have N separate optimization problems to run. So use a for loop;

% sp is 4 by 6 by N array
for i=1:N
  cvx_begin
  variable muler(4,4);
  minimize(norm(muler*xx-sp(:,:,i));
  cvx_end
 % Save the results or do whatever you want
end  :

Thank you very much!
It works, but I don’t know why the first form of code in my original question can’t work, it looks like the same as yours.
Anyway, I am glad it works.
Best wishes!
Thank you!

Your first post has muler(4,4)*xx where it should be muler*xx .
muler(4,4) is just a single element of the matrix, muler, not the entire matrix.

You also presumably want
mu(:,:,i)=muler;
rather than
mu(:,:,i)=muler(4,4);

Thank you
I’m just beginning to learn to use the CVX tools and I am reading “The CVX Users’ Guide” edited by Michael C. Grant, Stephen P. Boyd, it is useful. I made a mistake that led directly to the code can’t work. In Chinese it would be 差之毫厘谬以千里.
I will study hard and make progress!

I have another question about the former code.

for ii=1:6
cvx_begin
  variable muler(4,4);
  variable dc(4,6); 
  minimize(norm(muler*xx+dc-sp3(:,:,ii)));
  subject to
  muler==sp3(:,:,ii)*pinv(xx);
cvx_end
  mu(:,:,ii)=muler;
  dd(:,:,ii)=dc;
end

The code above is working well. But I need only one dc parameter. It is a least square problem in matrix domain. Y=k*x+b. There are many Y and x, assuming threr are 6 pair of data. I want to obtain only one k and one b. In my code, there will be one k and six b. The variable is muler(4,4) and variable dc(4,6), 6 is the cycle-index, I want to obtain a dc(4,1), how to define the variable dc? It should be a constant but need to solve.
Thank you!

I’m afraid I am I don’t understand what problem9s) you want to solve. You say you want only one k and b, but I don’t see either of them in your code…

You need to clearly define the mathematical problem(s) you wish to solve, and what the input data are for those problems. Do this for your own bemeffit, and hopefully the solution will become clear to you.

[quote=“DLQC, post:8, topic:7074, full:true”]
I have another question about the former code.

N=100;
for ii=1:N
cvx_begin
  variable muler(4,4);
  variable dc(4,6); 
  minimize(norm(muler*xx+dc-sp3(:,:,ii)));
  subject to
  muler==sp3(:,:,ii)*pinv(xx);
cvx_end
  mu(:,:,ii)=muler;
  dd(:,:,ii)=dc;
end

The code above is working well. It is a least square problem in matrix domain. sp3=mulerxx+dc. There are many sp3 and xx to calculate one muler and one dc, assuming threr are 6 pair of data. I want to obtain only one muler(4,4) and one dc(4,1), to keep the dimension same for subtraction minimize(norm(mulerxx+dc-sp3(:,:,ii)));,the variable is changed to muler(4,4) and variable dc(4,6). I want to obtain dc with 6 same columns.
I want to obtain a dc(4,1), how to define the variable dc with only 4 variables, the other 5 columns should be the same as the first column?

Thank you!

Does repmat do what you want?

variable dc(4,1)
repmat(dc,1,6) % this is a 4 by 6 matrix with all columns the same

This is the same as

variable dc(4,1)
[dc dc dc dc dc dc] % this is a 4 by 6 matrix with all columns the same
1 Like