How to create a matrix whose elements are function of their indices?

7 views (last 30 days)
I need to create a large square matrix, which I need to define elementwise. For that, each element of that matrix need to be a function of it's indices. I tried to do this, using two for loops, but that was taking too long. Is there a more faster way of doing this? If, there is some function like the 'matrix(m,n,f)' function of mupad, which returns a m×n matrix, whose element at the i'th row and j'th column is f(i,j). where f is a desired function, that would be the most apt one?
  15 Comments
Image Analyst
Image Analyst on 13 Jun 2018
You made a comment to Stephen Lord about his answer. Is that what you wanted to do? Or did you want your own independent answer?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 13 Jun 2018
Use meshgrid(). Try this (uses your f(x,y)=x^2+y^2 function):
rows = 10;
columns = 20;
startTime = tic; % Start timer.
% Use meshgrid to get x and y
[x, y] = meshgrid(1:columns, 1:rows);
% Create our output matrix, f:
f = x .^ 2 + y .^ 2
fprintf('Elapsed time was %f seconds.\n', toc(startTime));
f =
2 5 10 17 26 37 50 65 82 101 122 145 170 197 226 257 290 325 362 401
5 8 13 20 29 40 53 68 85 104 125 148 173 200 229 260 293 328 365 404
10 13 18 25 34 45 58 73 90 109 130 153 178 205 234 265 298 333 370 409
17 20 25 32 41 52 65 80 97 116 137 160 185 212 241 272 305 340 377 416
26 29 34 41 50 61 74 89 106 125 146 169 194 221 250 281 314 349 386 425
37 40 45 52 61 72 85 100 117 136 157 180 205 232 261 292 325 360 397 436
50 53 58 65 74 85 98 113 130 149 170 193 218 245 274 305 338 373 410 449
65 68 73 80 89 100 113 128 145 164 185 208 233 260 289 320 353 388 425 464
82 85 90 97 106 117 130 145 162 181 202 225 250 277 306 337 370 405 442 481
101 104 109 116 125 136 149 164 181 200 221 244 269 296 325 356 389 424 461 500
Elapsed time was 0.001304 seconds.
Adapt the number of rows and columns as needed.
  1 Comment
Saptarshi Biswas
Saptarshi Biswas on 14 Jun 2018
Thank you very much for suggesting the mesh hrid technique. It is working real quick. Although I haven't yet completeted implementing it, since some minute details are still to be adjusted in my code. But, I highly hope, this suggestion can resolve my problem completely.

Sign in to comment.

More Answers (4)

Matt J
Matt J on 13 Jun 2018
Edited: Matt J on 13 Jun 2018
There is no generically fast way to vectorize an arbitrary function f(). The speed you can achieve depends heavily on f() and how it is implemented.

Fangjun Jiang
Fangjun Jiang on 13 Jun 2018
I am not aware of such a built-in function in MATLAB without any special toolbox. A typical approach is to pre-allocate and then do a nested loop. You'll have to look at your desired function to see if there is any way to optimize it. Below is an example. The second approach takes less time because it re-uses the result and avoids duplicated calculation.
t=cputime;
f=@(x,y) x*x+y*y;
a=zeros(1300,1400);
for k=1:size(a,1)
for j=1:size(a,2)
a(k,j)=f(k,j);
end
end
cputime-t
%
t=cputime;
a=zeros(1300,1400);
f1=(1:size(a,1)).^2;
f2=(1:size(a,2)).^2;
for k=1:size(a,1)
for j=1:size(a,2)
a(k,j)=f1(k)+f2(j);
end
end
cputime-t
ans =
1.1094
ans =
0.0781

Steven Lord
Steven Lord on 13 Jun 2018
If your generating function uses only functions and/or operations that support implicit expansion, and you're using a release that supports implicit expansion, you could generate vectors of your row and column indices and use those implicit expansion operations. If your release predates implicit expansion but all your functions/operations operate element-wise, use meshgrid or ndgrid on those vectors of indices.
As examples see the hilb function or the private function minij used by the gallery function.
type hilb.m
type private/minij.m

Stephan
Stephan on 14 Jun 2018
For such calculations on this scale you should try without the symbolic toolbox.
If you look at the Wikipedia article for elliptic integral, you will find two potentially helpful hints:
The complete elliptic integral of the first kind is sometimes called the quarter period. It can be computed very efficiently in terms of the arithmetic–geometric mean:
The complete elliptic integral of the second kind can also be computed very efficiently using the arithmetic–geometric mean (Carlson 2010, 19.8).
Carlson, B. C. (2010), "Elliptic integral", in Olver, Frank W. J.; Lozier, Daniel M.; Boisvert, Ronald F.; Clark, Charles W., NIST Handbook of Mathematical Functions, Cambridge University Press, ISBN 978-0521192255, MR 2723248
Perhaps you can accelerate your calculations this way. I dont think you will get happy using symbolic toolbox in this case.
Also you could try
[K,E] = ellipke(m)
but i dont believe this will save much calculation time.
Best regards
Stephan

Community Treasure Hunt

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

Start Hunting!