Improving MATLAB Array Efficiency

Click For Summary
SUMMARY

The discussion focuses on optimizing MATLAB code for constructing a matrix without using nested for loops, significantly reducing execution time. The original code utilized two for loops to compute values based on 2xN arrays, resulting in high time costs. The updated solution employs MATLAB's matrix operations, specifically using meshgrid and sumsq functions, achieving a 99% reduction in time cost. The final code allows for scalability beyond 2xN matrices, enhancing efficiency and readability.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with matrix operations in MATLAB
  • Knowledge of logarithmic and norm functions in MATLAB
  • Experience with performance optimization techniques in coding
NEXT STEPS
  • Explore advanced MATLAB matrix manipulation techniques
  • Learn about the sumsq function and its applications
  • Investigate MATLAB's reshape and repmat functions for multidimensional arrays
  • Research performance profiling tools in MATLAB to identify bottlenecks
USEFUL FOR

MATLAB developers, data scientists, and researchers looking to enhance the efficiency of matrix computations and optimize their code performance.

Meurig
Messages
6
Reaction score
0
Hi all,

I have the following code:

M = zeros(N)
for i = 1:N
for j = 1:N
M(i,j)= log(norm(rs(:,i)-rc(:,j)))+log(norm(ri(:,i)-rc(:,j)));
end
end

Where rs, rc and ri are 2xN arrays.

Is there a way to construct the matrix without the use of the two for loops?
I'm trying to reduce the time cost of my code.

Thanks,
Meurig

UPDATE:

Have rewritten as:
[rsx,rcx] = meshgrid(rs(1,:),rc(1,:)');
[rsy,rcy] = meshgrid(rs(2,:),rc(2,:)');
M1 = (rsx-rcx).^2;
M2 = (rsy-rcy).^2;
[rix,rcx] = meshgrid(ri(1,:),rc(1,:)');
[riy,rcy] = meshgrid(ri(2,:),rc(2,:)');
M3 = (rix-rcx).^2;
M4 = (riy-rcy).^2;
Ms = log((M1+M2).^(1/2));
Mi = log((M3+M4).^(1/2));
M = Ms+Mi;

Reducing the time cost by 99%.

If anyone has a nicer way of writing this I'd be very interested though!
Cheers,
Meurig
 
Last edited:
Physics news on Phys.org
Quick note: your first and second version of the code have transposed results.

You can take advantage of Matlab's ability to work with matrices of N>2 dimensions. Reshape the matrices so that the rows are in the 3rd dimension, then repmat like you did (meshgrid is really just repmat done twice), and finally sum along dim=3. That way you don't need to “manually” deal with rows 1 and 2. And the code is no longer limited to 2xN matrices. If you later need to work with more than 2 rows, you don't have to change the code at all.

I used a function called sumsq to handle the squaring (makes it more readable, sumsq(x, N)=sum(x.*x, N)) and took out the square root out of the log:

Code:
[nr, nc]=size(rs);

rss=repmat(reshape(rs', 1, nc, nr), [nc 1 1]);
rii=repmat(reshape(ri', 1, nc, nr), [nc 1 1]);
rcc=repmat(reshape(rc', nc, 1, nr), [1 nc 1]);

M=(log(sumsq(rss-rcc, 3))+log(sumsq(rii-rcc, 3)))/2;
M=M';  % If you want M to look like the for-loop version
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 4 ·
Replies
4
Views
4K