help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Summing matrices that have been created in a loop


From: James Sherman Jr.
Subject: Re: Summing matrices that have been created in a loop
Date: Thu, 31 Jan 2013 07:55:06 -0500

On Thu, Jan 31, 2013 at 7:26 AM, Hannah Irons <address@hidden> wrote:
> Hi;
> I'm new to Octave and am struggling a bit. I've got the manual and have been
> searching for help in the forums but I think I need specific help.
>
> I have a 'for' loop that will construct matrices of a given size that I
> state before the loop.
>
> for
>
> ii=1:n
>
> % the matrix depends on the index ii, and some other parameters. but the
> size of the matrices created are independent of 'ii' so are all the same
> size. I won't give the actual formula that I'm using to construct the
> matrices because for what I'm stuck in its not important.
>
> Matrix=[0,ii;ii0]
>
> % for this case, my loop creates 2x2 matrices and there would be 'n' of them
> if it didn't overwrite the variable 'Matrix'.
>
> What I would like to do is either output all the matrices for all the values
> of 'ii', or have the loop deposit them in a sort or large matrix side by
> side. My eventual output would be a summation of the matrices created by the
> loop.
>
> I hope that I've made my problem clear, and any help would be appreciated.
>
>
>
> --
> View this message in context: 
> http://octave.1599824.n4.nabble.com/Summing-matrices-that-have-been-created-in-a-loop-tp4649307.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave

Hi Hannah,

I think what you're looking for is to simply make a three dimensional
array, something like:
=====
A = zeros(3,3,10);

for ii = 1:10,
   A(:, :, ii) = rand(3,3);
endfor

A_sum = sum(A, 3);
=====

where in the place of rand, you would put whatever your matrix
creation code is.  If you don't need to save the individual outputs
from each loop, you could also do:
=====
A_sum = zeros(3,3);

for ii = 1:10,
   A_sum += rand(3,3);
endfor
=====

There the += indicates that it takes the new matrix from rand adds it
to A_sum and saves it as a new A_sum.

Hope this helps,

James Sherman


reply via email to

[Prev in Thread] Current Thread [Next in Thread]