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

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

Hi,
do you know how big your matrix will be from the beginning? Did you preallocate with
zeros(n)
or
ones(n)
? If you didnt it takes a lot of time changing the size of the matrix by every run through your loop.
Maybe there is such function you want - i do not know such a function. But if you show us your code it could help to give useful hints.
Best regards
Stephan
Can you give an example of matrix(m,n,f)?
My matrix would be of dimension 1400×1400. And yes, I tried that too.
f(x,y)=x^2+y^2 A=matrix(2,3,f) returns, A= 2 5 10 5 8 13
https://in.mathworks.com/help/symbolic/mupad_ref/matrix.html I found that function in the above link.
For my understanding i have to ask: tried with the
matrix (m,n,f)
this takes too long?
I tried to use this function. But, when I try to execute it, matlab says undefined function.
The code above is what you want to do i guess
tic
A=zeros(1400);
for m = 1:1400
for n =1:1400
A(m,n) = m.^2+n.^2;
end
end
toc
takes 0.031684 sec on my mobile phone with matlab mobile...
Share your code please
Best regards
Stephan
N=input('enter the number of rings=')
miu0=4*pi*10^(-7); l=.001; d=.0001; r=6; a=1; z=1;
syms i j
p(i)=i*r/N-r/(2*N)
R=[]
for i=1:N
R(i,1)=p(i)
end
syms i j
k2(i,j)=4*p(i)*p(j)/(p(i)+p(j))^2
K(i,j)=ellipticK(k2(i,j))
E(i,j)=ellipticE(k2(i,j))
deff=l*tanh(d/l)
M=[]
for i=1:N
for j=1:N
if i==j
M(i,j)=p(i)*(log(8*p(i)*2*pi*N/r)-2)
else
M(i,j)=(p(i)+p(j))*((1-k2(i,j)/2)*K(i,j)-E(i,j))
end
end
end
This was like the code I wrote and it didn't ever completed for N=1400
Edit: comment moved to the answers area below.
Stephan, could you carry on the discussion in an official "Answer" down there with the rest of us? That way you could even get reputation points for it. Since Saptarshi hasn't responded to any of the 4 other answers, I think he may only be seeing yours because he didn't scroll down far enough.
Thanks for the hint Image Analyst. Made as suggested.
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?
You are right again... ;-)
Has already been late yesterday...

Sign in to comment.

 Accepted Answer

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

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.
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
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
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!