How to compile large matrix

Hello!
I am trying compile a matrix with one row and a large number of columns (matrix A). I have a problem with compiling matrix A, which contains only three different values, as an example
A = [a,b,c,a,b,c,a,b,c,...]
and so on. I tried with repmat command, but because there is a lot of data (size of matrix A is 1x600.000 or with even more columns), Matlab needs to a lot of time to calculate.
I need this matrix in for loop to multiply it with another matrix (matrix B), which has a large number or rows and only 4 columns. I have compile matrix B in for loop.
Below there is a matlab code I have use.
Thank you in advance for all the answers. Tamara
%data = matrix with 3 columns and a lot of rows (the number of rows varies according to the number of scanned points, number of columns is always the same, xyz coordinate)
%plane_par = matrix with 1 column and 4 rows (size is always the same, they are plane parameters)
size_data = size(data);
size_repmat = size_data(1,1);
Adel = [plane_par(1,1), plane_par(2,1), plane_par(3,1)];
A = repmat(Adel,1,size_repmat);
for i = 1:size(data,1)
B = [data(i,1), data(i,2), data(i,3), 1];
f = [-plane_par(1,1)*data(i,1)-plane_par(2,1)*data(i,2)-plane_par(3,1)*data(i,3)+plane_par(4,1)];
delta = pinv(B'*pinv(A*A')*B)*(B'*pinv(A*A')*f);
end

3 个评论

"lot of time" how much is that?
>> tic, M = repmat( [1,2,3], 1, 6e5 ); toc
Elapsed time is 0.003957 seconds.
>> tic, M = repmat( {'a','b','c'}, 1, 6e5 ); toc
Elapsed time is 0.034483 seconds.
btw: do a, b, c stand for numbers?
It means even half an hour. I think that the problem is when Matlab must multiply matrix A and B, where matrix A has around 600.000 columns. Yes, parameters a,b,c are numbers.
Note that delta is overwritten inside the loop.
Break out pinv(A*A') from the expression, pinv(B'*pinv(A*A')*B)*(B'*pinv(A*A')*f) and calculate it outside the loop. A doesn't change in the loop.

请先登录,再进行评论。

回答(1 个)

for your case:
s = size(data,1);
B = [data,ones(s,1)]';
BB = permute(B,[3,1,2]).*permute(B,[1,3,2]);
f = B.*(B'*(plane_par.*[-1;-1;-1;1]))';
delta = zeros(4,s);
for ii = 1:s
delta(:,ii) = pinv(BB(:,:,ii))*f(:,ii);
end

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by