Is there a way to do mpower on a matrix repmat array?Recursive Anonymous Function MatlabLoopless function calls on vector/matrix members in Matlab/OctaveCreating new matrix from existing oneOptimizing code, removing “for loop”Matlab complex matrix magnitudeMATLAB How to get rid of loop for subtracting two arraysHow to component wise multiply a vector and a matrix in MATLAB?(matlab matrix operation), Is it possible to get a group of value from matrix without loop?Efficient implementation of a sequence of matrix-vector products / specific “tensor”-matrix productFor-loop bottleneck (outer products and matrix multiplications in slices of an array of d >2)block matrix multiplication with matlab cells

Users forgotting to regenerate PDF before sending it

As a supervisor, what feedback would you expect from a PhD who quits?

Moving millions of files to a different directory with specfic name patterns

How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant

Can one block with a protection from color creature?

How does one acquire an undead eyeball encased in a gem?

Why won't the U.S. sign a peace treaty with North Korea?

How many Jimmys can fit?

Mtg creature spells, instants, priority?

What is this strange structure on a mountain top in the Italian Alps?

Is it ok for parents to kiss and romance with each other while their 2- to 8-year-old child watches?

Where are the Wazirs?

Publishing papers seem natural to many, while I find it really hard to think novel stuff to pursue till publication. How to cope up with this?

How should I ask for a "pint" in countries that use metric?

How to slice a string input at a certain unknown index

My professor has told me he will be the corresponding author. Will it hurt my future career?

Writing an ace/aro character?

My previous employer committed a severe violation of the law and is also being sued by me. How do I explain the situation to future employers?

What are the consequences for a developed nation to not accept any refugees?

Why do people prefer metropolitan areas, considering monsters and villains?

Why do airports remove/realign runways?

Would denouncing cheaters from an exam make me less likely to receive penalties?

Array or vector? Two dimensional array or matrix?

Can a landlord force all residents to use the landlord's in-house debit card accounts?



Is there a way to do mpower on a matrix repmat array?


Recursive Anonymous Function MatlabLoopless function calls on vector/matrix members in Matlab/OctaveCreating new matrix from existing oneOptimizing code, removing “for loop”Matlab complex matrix magnitudeMATLAB How to get rid of loop for subtracting two arraysHow to component wise multiply a vector and a matrix in MATLAB?(matlab matrix operation), Is it possible to get a group of value from matrix without loop?Efficient implementation of a sequence of matrix-vector products / specific “tensor”-matrix productFor-loop bottleneck (outer products and matrix multiplications in slices of an array of d >2)block matrix multiplication with matlab cells






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I wonder if there is a way raise power of a matrix A as an array?



Assume that we have this matrix



A =

5 4
3 6


Then we repeat its shape.



>> repmat(A, 5, 1)
ans =

5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6


Now I want to rase the power so the long repeated matrix looks like this:



>> [A^1; A^2; A^3; A^4; A^5]
ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


Is it possible to do that without a for loop in MATLAB/Octave?










share|improve this question



















  • 1





    MATLAB or Octave? Please pick one. Answers might not be the same. Notice the tag usage guideline: "Don’t use both the [matlab] and [octave] tags, unless the question is explicitly about the similarities or differences between the two."

    – Cris Luengo
    Mar 25 at 22:29











  • Also, why do you want to avoid a loop? Is this part of your code a bottleneck?

    – Cris Luengo
    Mar 25 at 22:30











  • @CrisLuengo It does not matter. Matlab and Octave have the same syntax and Octave can do MATLAB code.

    – Heretic
    Mar 25 at 22:35






  • 4





    For loops are not at all slow any more in MATLAB, and many vectorization efforts actually slow down execution. Don't vectorize until you've programmed the simple solution and seen it is too slow for your application. Otherwise you're just wasting time. And you need to be able to compare the timing of your vectorized code, to make sure you are actually picking the fastest code.

    – Cris Luengo
    Mar 25 at 23:04






  • 3





    Octave has similar syntax to MATLAB, but not identical. Octave also lacks a lot of the functions that are available in MATLAB, and it has some functions that do not exist in MATLAB. Octave cannot do a lot of MATLAB code I have written, and MATLAB cannot do a lot of Octave code people have posted in their questions here.

    – Cris Luengo
    Mar 25 at 23:05

















2















I wonder if there is a way raise power of a matrix A as an array?



Assume that we have this matrix



A =

5 4
3 6


Then we repeat its shape.



>> repmat(A, 5, 1)
ans =

5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6


Now I want to rase the power so the long repeated matrix looks like this:



>> [A^1; A^2; A^3; A^4; A^5]
ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


Is it possible to do that without a for loop in MATLAB/Octave?










share|improve this question



















  • 1





    MATLAB or Octave? Please pick one. Answers might not be the same. Notice the tag usage guideline: "Don’t use both the [matlab] and [octave] tags, unless the question is explicitly about the similarities or differences between the two."

    – Cris Luengo
    Mar 25 at 22:29











  • Also, why do you want to avoid a loop? Is this part of your code a bottleneck?

    – Cris Luengo
    Mar 25 at 22:30











  • @CrisLuengo It does not matter. Matlab and Octave have the same syntax and Octave can do MATLAB code.

    – Heretic
    Mar 25 at 22:35






  • 4





    For loops are not at all slow any more in MATLAB, and many vectorization efforts actually slow down execution. Don't vectorize until you've programmed the simple solution and seen it is too slow for your application. Otherwise you're just wasting time. And you need to be able to compare the timing of your vectorized code, to make sure you are actually picking the fastest code.

    – Cris Luengo
    Mar 25 at 23:04






  • 3





    Octave has similar syntax to MATLAB, but not identical. Octave also lacks a lot of the functions that are available in MATLAB, and it has some functions that do not exist in MATLAB. Octave cannot do a lot of MATLAB code I have written, and MATLAB cannot do a lot of Octave code people have posted in their questions here.

    – Cris Luengo
    Mar 25 at 23:05













2












2








2








I wonder if there is a way raise power of a matrix A as an array?



Assume that we have this matrix



A =

5 4
3 6


Then we repeat its shape.



>> repmat(A, 5, 1)
ans =

5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6


Now I want to rase the power so the long repeated matrix looks like this:



>> [A^1; A^2; A^3; A^4; A^5]
ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


Is it possible to do that without a for loop in MATLAB/Octave?










share|improve this question
















I wonder if there is a way raise power of a matrix A as an array?



Assume that we have this matrix



A =

5 4
3 6


Then we repeat its shape.



>> repmat(A, 5, 1)
ans =

5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6
5 4
3 6


Now I want to rase the power so the long repeated matrix looks like this:



>> [A^1; A^2; A^3; A^4; A^5]
ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


Is it possible to do that without a for loop in MATLAB/Octave?







matlab vectorization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 7:37







Heretic

















asked Mar 25 at 22:26









HereticHeretic

558 bronze badges




558 bronze badges







  • 1





    MATLAB or Octave? Please pick one. Answers might not be the same. Notice the tag usage guideline: "Don’t use both the [matlab] and [octave] tags, unless the question is explicitly about the similarities or differences between the two."

    – Cris Luengo
    Mar 25 at 22:29











  • Also, why do you want to avoid a loop? Is this part of your code a bottleneck?

    – Cris Luengo
    Mar 25 at 22:30











  • @CrisLuengo It does not matter. Matlab and Octave have the same syntax and Octave can do MATLAB code.

    – Heretic
    Mar 25 at 22:35






  • 4





    For loops are not at all slow any more in MATLAB, and many vectorization efforts actually slow down execution. Don't vectorize until you've programmed the simple solution and seen it is too slow for your application. Otherwise you're just wasting time. And you need to be able to compare the timing of your vectorized code, to make sure you are actually picking the fastest code.

    – Cris Luengo
    Mar 25 at 23:04






  • 3





    Octave has similar syntax to MATLAB, but not identical. Octave also lacks a lot of the functions that are available in MATLAB, and it has some functions that do not exist in MATLAB. Octave cannot do a lot of MATLAB code I have written, and MATLAB cannot do a lot of Octave code people have posted in their questions here.

    – Cris Luengo
    Mar 25 at 23:05












  • 1





    MATLAB or Octave? Please pick one. Answers might not be the same. Notice the tag usage guideline: "Don’t use both the [matlab] and [octave] tags, unless the question is explicitly about the similarities or differences between the two."

    – Cris Luengo
    Mar 25 at 22:29











  • Also, why do you want to avoid a loop? Is this part of your code a bottleneck?

    – Cris Luengo
    Mar 25 at 22:30











  • @CrisLuengo It does not matter. Matlab and Octave have the same syntax and Octave can do MATLAB code.

    – Heretic
    Mar 25 at 22:35






  • 4





    For loops are not at all slow any more in MATLAB, and many vectorization efforts actually slow down execution. Don't vectorize until you've programmed the simple solution and seen it is too slow for your application. Otherwise you're just wasting time. And you need to be able to compare the timing of your vectorized code, to make sure you are actually picking the fastest code.

    – Cris Luengo
    Mar 25 at 23:04






  • 3





    Octave has similar syntax to MATLAB, but not identical. Octave also lacks a lot of the functions that are available in MATLAB, and it has some functions that do not exist in MATLAB. Octave cannot do a lot of MATLAB code I have written, and MATLAB cannot do a lot of Octave code people have posted in their questions here.

    – Cris Luengo
    Mar 25 at 23:05







1




1





MATLAB or Octave? Please pick one. Answers might not be the same. Notice the tag usage guideline: "Don’t use both the [matlab] and [octave] tags, unless the question is explicitly about the similarities or differences between the two."

– Cris Luengo
Mar 25 at 22:29





MATLAB or Octave? Please pick one. Answers might not be the same. Notice the tag usage guideline: "Don’t use both the [matlab] and [octave] tags, unless the question is explicitly about the similarities or differences between the two."

– Cris Luengo
Mar 25 at 22:29













Also, why do you want to avoid a loop? Is this part of your code a bottleneck?

– Cris Luengo
Mar 25 at 22:30





Also, why do you want to avoid a loop? Is this part of your code a bottleneck?

– Cris Luengo
Mar 25 at 22:30













@CrisLuengo It does not matter. Matlab and Octave have the same syntax and Octave can do MATLAB code.

– Heretic
Mar 25 at 22:35





@CrisLuengo It does not matter. Matlab and Octave have the same syntax and Octave can do MATLAB code.

– Heretic
Mar 25 at 22:35




4




4





For loops are not at all slow any more in MATLAB, and many vectorization efforts actually slow down execution. Don't vectorize until you've programmed the simple solution and seen it is too slow for your application. Otherwise you're just wasting time. And you need to be able to compare the timing of your vectorized code, to make sure you are actually picking the fastest code.

– Cris Luengo
Mar 25 at 23:04





For loops are not at all slow any more in MATLAB, and many vectorization efforts actually slow down execution. Don't vectorize until you've programmed the simple solution and seen it is too slow for your application. Otherwise you're just wasting time. And you need to be able to compare the timing of your vectorized code, to make sure you are actually picking the fastest code.

– Cris Luengo
Mar 25 at 23:04




3




3





Octave has similar syntax to MATLAB, but not identical. Octave also lacks a lot of the functions that are available in MATLAB, and it has some functions that do not exist in MATLAB. Octave cannot do a lot of MATLAB code I have written, and MATLAB cannot do a lot of Octave code people have posted in their questions here.

– Cris Luengo
Mar 25 at 23:05





Octave has similar syntax to MATLAB, but not identical. Octave also lacks a lot of the functions that are available in MATLAB, and it has some functions that do not exist in MATLAB. Octave cannot do a lot of MATLAB code I have written, and MATLAB cannot do a lot of Octave code people have posted in their questions here.

– Cris Luengo
Mar 25 at 23:05












3 Answers
3






active

oldest

votes


















3














Another option using arrayfun



B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).')


Result:



B =
5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


But a basic for-loop would probably be the fastest option in this case.



Benchmarking with octave:



tic
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);
for ii = 1:1000
% Calculate for arbitrary n.
nPower(A, 5);
end
toc


Elapsed time is 1.023 seconds.



tic
for ii = 1:1000
B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).');
end
toc


Elapsed time is 4.8684 seconds.



tic
for ii = 1:1000
B=[];
for jj = 1:5
B = [B;A^jj];
end
end
toc


Elapsed time is 0.039371 seconds






share|improve this answer




















  • 1





    arrayfun is a loop. The tags indicate that vectorization is required

    – Sardar Usama
    Mar 26 at 9:56











  • Using arrayfun was also my first idea. Principally, I agree on using loops for that task, as also initially stated by Cris Luengo in the above comments.

    – HansHirse
    Mar 26 at 10:15


















3














EDIT



To also mention this in my answer: Recursion is not vectorization as of Matlab/Octave users usually might think of. I just had the idea of a recursive, anonymous function in my mind, and found the given task a nice small example to test the referenced solution on.




I was looking for recursive, anonymous functions and found this great answer. I incorporated the ideas from there to meet the expectations stated in the question, and came to this short code snippet.



% Input.
A = [5 4; 3 6]

% Set up recursive anonymous function.
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);

% Calculate for arbitrary n.
nPower(A, 5)


For explanations, please have a look at the linked answer.



Output:



A =

5 4
3 6

ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756





share|improve this answer




















  • 1





    Please note that recursion is not vectorization.

    – Sardar Usama
    Mar 26 at 10:07











  • @SardarUsama I know, and actually I wanted to mention it in my answer. Generally, I don't think there is a "plain" vectorization possibility in terms of Matlab/Octave users usually think of. Nevertheless, you're right.

    – HansHirse
    Mar 26 at 10:10



















2














If you really want to use vectorization (which is IMO overkill in this case), you could also use the property:



A^n = P*D^n*P^-1 %A SHOULD BE a diagonalizable matrix


Where



[P,D] = eig(A) %the eigenvectors and eigenvalue


So



A = [5 4; 3 6]
n = 5;
% get the eigenvalue/eigenvector
[P,D]=eig(A);
% create the intermediate matrix
MD = diag(D).^[1:n];
MD = diag(MD(:));
% get the result
SP = kron(eye(n,n),P)*MD*kron(eye(n,n),P^-1);


With:



SP =

5 4 0 0 0 0 0 0 0 0
3 6 0 0 0 0 0 0 0 0
0 0 37 44 0 0 0 0 0 0
0 0 33 48 0 0 0 0 0 0
0 0 0 0 317 412 0 0 0 0
0 0 0 0 309 420 0 0 0 0
0 0 0 0 0 0 2821 3740 0 0
0 0 0 0 0 0 2805 3756 0 0
0 0 0 0 0 0 0 0 25325 33724
0 0 0 0 0 0 0 0 25293 33756


You still just need to extract those values. It could be interesting to use sparse matrix in this case to reduce memory usage.



Something like this:



SP = sparse(kron(eye(n,n),P))*sparse(MD)*sparse(kron(eye(n,n),P^-1));





share|improve this answer

























  • You should add this to your timing in the other answer.

    – Cris Luengo
    Mar 26 at 12:27













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55347294%2fis-there-a-way-to-do-mpower-on-a-matrix-repmat-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Another option using arrayfun



B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).')


Result:



B =
5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


But a basic for-loop would probably be the fastest option in this case.



Benchmarking with octave:



tic
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);
for ii = 1:1000
% Calculate for arbitrary n.
nPower(A, 5);
end
toc


Elapsed time is 1.023 seconds.



tic
for ii = 1:1000
B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).');
end
toc


Elapsed time is 4.8684 seconds.



tic
for ii = 1:1000
B=[];
for jj = 1:5
B = [B;A^jj];
end
end
toc


Elapsed time is 0.039371 seconds






share|improve this answer




















  • 1





    arrayfun is a loop. The tags indicate that vectorization is required

    – Sardar Usama
    Mar 26 at 9:56











  • Using arrayfun was also my first idea. Principally, I agree on using loops for that task, as also initially stated by Cris Luengo in the above comments.

    – HansHirse
    Mar 26 at 10:15















3














Another option using arrayfun



B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).')


Result:



B =
5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


But a basic for-loop would probably be the fastest option in this case.



Benchmarking with octave:



tic
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);
for ii = 1:1000
% Calculate for arbitrary n.
nPower(A, 5);
end
toc


Elapsed time is 1.023 seconds.



tic
for ii = 1:1000
B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).');
end
toc


Elapsed time is 4.8684 seconds.



tic
for ii = 1:1000
B=[];
for jj = 1:5
B = [B;A^jj];
end
end
toc


Elapsed time is 0.039371 seconds






share|improve this answer




















  • 1





    arrayfun is a loop. The tags indicate that vectorization is required

    – Sardar Usama
    Mar 26 at 9:56











  • Using arrayfun was also my first idea. Principally, I agree on using loops for that task, as also initially stated by Cris Luengo in the above comments.

    – HansHirse
    Mar 26 at 10:15













3












3








3







Another option using arrayfun



B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).')


Result:



B =
5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


But a basic for-loop would probably be the fastest option in this case.



Benchmarking with octave:



tic
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);
for ii = 1:1000
% Calculate for arbitrary n.
nPower(A, 5);
end
toc


Elapsed time is 1.023 seconds.



tic
for ii = 1:1000
B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).');
end
toc


Elapsed time is 4.8684 seconds.



tic
for ii = 1:1000
B=[];
for jj = 1:5
B = [B;A^jj];
end
end
toc


Elapsed time is 0.039371 seconds






share|improve this answer















Another option using arrayfun



B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).')


Result:



B =
5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756


But a basic for-loop would probably be the fastest option in this case.



Benchmarking with octave:



tic
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);
for ii = 1:1000
% Calculate for arbitrary n.
nPower(A, 5);
end
toc


Elapsed time is 1.023 seconds.



tic
for ii = 1:1000
B = cell2mat(arrayfun(@(x)A^x,1:5,'UniformOutput',0).');
end
toc


Elapsed time is 4.8684 seconds.



tic
for ii = 1:1000
B=[];
for jj = 1:5
B = [B;A^jj];
end
end
toc


Elapsed time is 0.039371 seconds







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 9:57

























answered Mar 26 at 9:35









obchardonobchardon

4,7361 gold badge8 silver badges22 bronze badges




4,7361 gold badge8 silver badges22 bronze badges







  • 1





    arrayfun is a loop. The tags indicate that vectorization is required

    – Sardar Usama
    Mar 26 at 9:56











  • Using arrayfun was also my first idea. Principally, I agree on using loops for that task, as also initially stated by Cris Luengo in the above comments.

    – HansHirse
    Mar 26 at 10:15












  • 1





    arrayfun is a loop. The tags indicate that vectorization is required

    – Sardar Usama
    Mar 26 at 9:56











  • Using arrayfun was also my first idea. Principally, I agree on using loops for that task, as also initially stated by Cris Luengo in the above comments.

    – HansHirse
    Mar 26 at 10:15







1




1





arrayfun is a loop. The tags indicate that vectorization is required

– Sardar Usama
Mar 26 at 9:56





arrayfun is a loop. The tags indicate that vectorization is required

– Sardar Usama
Mar 26 at 9:56













Using arrayfun was also my first idea. Principally, I agree on using loops for that task, as also initially stated by Cris Luengo in the above comments.

– HansHirse
Mar 26 at 10:15





Using arrayfun was also my first idea. Principally, I agree on using loops for that task, as also initially stated by Cris Luengo in the above comments.

– HansHirse
Mar 26 at 10:15













3














EDIT



To also mention this in my answer: Recursion is not vectorization as of Matlab/Octave users usually might think of. I just had the idea of a recursive, anonymous function in my mind, and found the given task a nice small example to test the referenced solution on.




I was looking for recursive, anonymous functions and found this great answer. I incorporated the ideas from there to meet the expectations stated in the question, and came to this short code snippet.



% Input.
A = [5 4; 3 6]

% Set up recursive anonymous function.
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);

% Calculate for arbitrary n.
nPower(A, 5)


For explanations, please have a look at the linked answer.



Output:



A =

5 4
3 6

ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756





share|improve this answer




















  • 1





    Please note that recursion is not vectorization.

    – Sardar Usama
    Mar 26 at 10:07











  • @SardarUsama I know, and actually I wanted to mention it in my answer. Generally, I don't think there is a "plain" vectorization possibility in terms of Matlab/Octave users usually think of. Nevertheless, you're right.

    – HansHirse
    Mar 26 at 10:10
















3














EDIT



To also mention this in my answer: Recursion is not vectorization as of Matlab/Octave users usually might think of. I just had the idea of a recursive, anonymous function in my mind, and found the given task a nice small example to test the referenced solution on.




I was looking for recursive, anonymous functions and found this great answer. I incorporated the ideas from there to meet the expectations stated in the question, and came to this short code snippet.



% Input.
A = [5 4; 3 6]

% Set up recursive anonymous function.
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);

% Calculate for arbitrary n.
nPower(A, 5)


For explanations, please have a look at the linked answer.



Output:



A =

5 4
3 6

ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756





share|improve this answer




















  • 1





    Please note that recursion is not vectorization.

    – Sardar Usama
    Mar 26 at 10:07











  • @SardarUsama I know, and actually I wanted to mention it in my answer. Generally, I don't think there is a "plain" vectorization possibility in terms of Matlab/Octave users usually think of. Nevertheless, you're right.

    – HansHirse
    Mar 26 at 10:10














3












3








3







EDIT



To also mention this in my answer: Recursion is not vectorization as of Matlab/Octave users usually might think of. I just had the idea of a recursive, anonymous function in my mind, and found the given task a nice small example to test the referenced solution on.




I was looking for recursive, anonymous functions and found this great answer. I incorporated the ideas from there to meet the expectations stated in the question, and came to this short code snippet.



% Input.
A = [5 4; 3 6]

% Set up recursive anonymous function.
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);

% Calculate for arbitrary n.
nPower(A, 5)


For explanations, please have a look at the linked answer.



Output:



A =

5 4
3 6

ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756





share|improve this answer















EDIT



To also mention this in my answer: Recursion is not vectorization as of Matlab/Octave users usually might think of. I just had the idea of a recursive, anonymous function in my mind, and found the given task a nice small example to test the referenced solution on.




I was looking for recursive, anonymous functions and found this great answer. I incorporated the ideas from there to meet the expectations stated in the question, and came to this short code snippet.



% Input.
A = [5 4; 3 6]

% Set up recursive anonymous function.
iif = @(varargin) varargin2*find([varargin1:2:end], 1, 'first')();
recPower = @(A, B, n, f) iif(n > 1, @() [B; f(A, A * B, n - 1, f)], true, @() B);
nPower = @(A, n) recPower(A, A, n, recPower);

% Calculate for arbitrary n.
nPower(A, 5)


For explanations, please have a look at the linked answer.



Output:



A =

5 4
3 6

ans =

5 4
3 6
37 44
33 48
317 412
309 420
2821 3740
2805 3756
25325 33724
25293 33756






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 10:19

























answered Mar 26 at 8:01









HansHirseHansHirse

3,1685 gold badges11 silver badges31 bronze badges




3,1685 gold badges11 silver badges31 bronze badges







  • 1





    Please note that recursion is not vectorization.

    – Sardar Usama
    Mar 26 at 10:07











  • @SardarUsama I know, and actually I wanted to mention it in my answer. Generally, I don't think there is a "plain" vectorization possibility in terms of Matlab/Octave users usually think of. Nevertheless, you're right.

    – HansHirse
    Mar 26 at 10:10













  • 1





    Please note that recursion is not vectorization.

    – Sardar Usama
    Mar 26 at 10:07











  • @SardarUsama I know, and actually I wanted to mention it in my answer. Generally, I don't think there is a "plain" vectorization possibility in terms of Matlab/Octave users usually think of. Nevertheless, you're right.

    – HansHirse
    Mar 26 at 10:10








1




1





Please note that recursion is not vectorization.

– Sardar Usama
Mar 26 at 10:07





Please note that recursion is not vectorization.

– Sardar Usama
Mar 26 at 10:07













@SardarUsama I know, and actually I wanted to mention it in my answer. Generally, I don't think there is a "plain" vectorization possibility in terms of Matlab/Octave users usually think of. Nevertheless, you're right.

– HansHirse
Mar 26 at 10:10






@SardarUsama I know, and actually I wanted to mention it in my answer. Generally, I don't think there is a "plain" vectorization possibility in terms of Matlab/Octave users usually think of. Nevertheless, you're right.

– HansHirse
Mar 26 at 10:10












2














If you really want to use vectorization (which is IMO overkill in this case), you could also use the property:



A^n = P*D^n*P^-1 %A SHOULD BE a diagonalizable matrix


Where



[P,D] = eig(A) %the eigenvectors and eigenvalue


So



A = [5 4; 3 6]
n = 5;
% get the eigenvalue/eigenvector
[P,D]=eig(A);
% create the intermediate matrix
MD = diag(D).^[1:n];
MD = diag(MD(:));
% get the result
SP = kron(eye(n,n),P)*MD*kron(eye(n,n),P^-1);


With:



SP =

5 4 0 0 0 0 0 0 0 0
3 6 0 0 0 0 0 0 0 0
0 0 37 44 0 0 0 0 0 0
0 0 33 48 0 0 0 0 0 0
0 0 0 0 317 412 0 0 0 0
0 0 0 0 309 420 0 0 0 0
0 0 0 0 0 0 2821 3740 0 0
0 0 0 0 0 0 2805 3756 0 0
0 0 0 0 0 0 0 0 25325 33724
0 0 0 0 0 0 0 0 25293 33756


You still just need to extract those values. It could be interesting to use sparse matrix in this case to reduce memory usage.



Something like this:



SP = sparse(kron(eye(n,n),P))*sparse(MD)*sparse(kron(eye(n,n),P^-1));





share|improve this answer

























  • You should add this to your timing in the other answer.

    – Cris Luengo
    Mar 26 at 12:27















2














If you really want to use vectorization (which is IMO overkill in this case), you could also use the property:



A^n = P*D^n*P^-1 %A SHOULD BE a diagonalizable matrix


Where



[P,D] = eig(A) %the eigenvectors and eigenvalue


So



A = [5 4; 3 6]
n = 5;
% get the eigenvalue/eigenvector
[P,D]=eig(A);
% create the intermediate matrix
MD = diag(D).^[1:n];
MD = diag(MD(:));
% get the result
SP = kron(eye(n,n),P)*MD*kron(eye(n,n),P^-1);


With:



SP =

5 4 0 0 0 0 0 0 0 0
3 6 0 0 0 0 0 0 0 0
0 0 37 44 0 0 0 0 0 0
0 0 33 48 0 0 0 0 0 0
0 0 0 0 317 412 0 0 0 0
0 0 0 0 309 420 0 0 0 0
0 0 0 0 0 0 2821 3740 0 0
0 0 0 0 0 0 2805 3756 0 0
0 0 0 0 0 0 0 0 25325 33724
0 0 0 0 0 0 0 0 25293 33756


You still just need to extract those values. It could be interesting to use sparse matrix in this case to reduce memory usage.



Something like this:



SP = sparse(kron(eye(n,n),P))*sparse(MD)*sparse(kron(eye(n,n),P^-1));





share|improve this answer

























  • You should add this to your timing in the other answer.

    – Cris Luengo
    Mar 26 at 12:27













2












2








2







If you really want to use vectorization (which is IMO overkill in this case), you could also use the property:



A^n = P*D^n*P^-1 %A SHOULD BE a diagonalizable matrix


Where



[P,D] = eig(A) %the eigenvectors and eigenvalue


So



A = [5 4; 3 6]
n = 5;
% get the eigenvalue/eigenvector
[P,D]=eig(A);
% create the intermediate matrix
MD = diag(D).^[1:n];
MD = diag(MD(:));
% get the result
SP = kron(eye(n,n),P)*MD*kron(eye(n,n),P^-1);


With:



SP =

5 4 0 0 0 0 0 0 0 0
3 6 0 0 0 0 0 0 0 0
0 0 37 44 0 0 0 0 0 0
0 0 33 48 0 0 0 0 0 0
0 0 0 0 317 412 0 0 0 0
0 0 0 0 309 420 0 0 0 0
0 0 0 0 0 0 2821 3740 0 0
0 0 0 0 0 0 2805 3756 0 0
0 0 0 0 0 0 0 0 25325 33724
0 0 0 0 0 0 0 0 25293 33756


You still just need to extract those values. It could be interesting to use sparse matrix in this case to reduce memory usage.



Something like this:



SP = sparse(kron(eye(n,n),P))*sparse(MD)*sparse(kron(eye(n,n),P^-1));





share|improve this answer















If you really want to use vectorization (which is IMO overkill in this case), you could also use the property:



A^n = P*D^n*P^-1 %A SHOULD BE a diagonalizable matrix


Where



[P,D] = eig(A) %the eigenvectors and eigenvalue


So



A = [5 4; 3 6]
n = 5;
% get the eigenvalue/eigenvector
[P,D]=eig(A);
% create the intermediate matrix
MD = diag(D).^[1:n];
MD = diag(MD(:));
% get the result
SP = kron(eye(n,n),P)*MD*kron(eye(n,n),P^-1);


With:



SP =

5 4 0 0 0 0 0 0 0 0
3 6 0 0 0 0 0 0 0 0
0 0 37 44 0 0 0 0 0 0
0 0 33 48 0 0 0 0 0 0
0 0 0 0 317 412 0 0 0 0
0 0 0 0 309 420 0 0 0 0
0 0 0 0 0 0 2821 3740 0 0
0 0 0 0 0 0 2805 3756 0 0
0 0 0 0 0 0 0 0 25325 33724
0 0 0 0 0 0 0 0 25293 33756


You still just need to extract those values. It could be interesting to use sparse matrix in this case to reduce memory usage.



Something like this:



SP = sparse(kron(eye(n,n),P))*sparse(MD)*sparse(kron(eye(n,n),P^-1));






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 13:10

























answered Mar 26 at 11:39









obchardonobchardon

4,7361 gold badge8 silver badges22 bronze badges




4,7361 gold badge8 silver badges22 bronze badges












  • You should add this to your timing in the other answer.

    – Cris Luengo
    Mar 26 at 12:27

















  • You should add this to your timing in the other answer.

    – Cris Luengo
    Mar 26 at 12:27
















You should add this to your timing in the other answer.

– Cris Luengo
Mar 26 at 12:27





You should add this to your timing in the other answer.

– Cris Luengo
Mar 26 at 12:27

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55347294%2fis-there-a-way-to-do-mpower-on-a-matrix-repmat-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript