Improving MATLAB Array Efficiency

In summary, the code reshapes the matrices to work with a 3D grid and then sums the rows together. This saves a lot of time compared to the original code.
  • #1
Meurig
6
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
  • #2
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
 

Related to Improving MATLAB Array Efficiency

1. How can I improve the efficiency of my MATLAB arrays?

There are several ways to improve the efficiency of your MATLAB arrays, such as preallocating memory, avoiding unnecessary loops, and using vectorized operations.

2. What is preallocation and how does it improve array efficiency?

Preallocation is the process of reserving memory for an array before it is filled with data. This can improve efficiency by avoiding the need for MATLAB to repeatedly allocate and reallocate memory as the array grows in size.

3. What are vectorized operations and how do they improve array efficiency?

Vectorized operations involve performing calculations on entire arrays instead of individual elements, which can greatly improve efficiency compared to using loops. This is because MATLAB is optimized for vector operations.

4. Are there any built-in functions in MATLAB that can improve array efficiency?

Yes, MATLAB has several built-in functions that can improve array efficiency, such as repmat for replicating arrays, accumarray for performing calculations on subsets of an array, and bsxfun for performing element-wise operations on arrays with different dimensions.

5. How can I measure the efficiency of my MATLAB arrays?

One way to measure array efficiency is by using the tic and toc functions to time the execution of different code snippets. You can also use the MATLAB profiler to identify any bottlenecks or areas for improvement in your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • Introductory Physics Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
8K
Back
Top