How to create a matrix whose elements are function of their indices?
Show older comments
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
Fangjun Jiang
on 13 Jun 2018
Can you give an example of matrix(m,n,f)?
Saptarshi Biswas
on 13 Jun 2018
Saptarshi Biswas
on 13 Jun 2018
Saptarshi Biswas
on 13 Jun 2018
For my understanding i have to ask: tried with the
matrix (m,n,f)
this takes too long?
Saptarshi Biswas
on 13 Jun 2018
Fangjun Jiang
on 13 Jun 2018
It needs the Symbolic toolbox
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
Saptarshi Biswas
on 13 Jun 2018
Image Analyst
on 13 Jun 2018
Edited: Image Analyst
on 13 Jun 2018
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.
Stephan
on 13 Jun 2018
Thanks for the hint Image Analyst. Made as suggested.
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?
Stephan
on 14 Jun 2018
You are right again... ;-)
Has already been late yesterday...
Accepted Answer
More Answers (4)
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
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
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
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
1 Comment
Saptarshi Biswas
on 14 Jun 2018
Categories
Find more on Code Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!