How to sum over indexed arrays

I have 3 arrays of the same dimension, having the same name but with indexes. How can one sum over the elements.
clear all; close all;
It=3; %Number of created arrays
for ii=1:It
a(ii) = [1 1 rand()]
end
abc = zeros(size(a(1))); %array abc has zero entries of same dimension as the a-arrays
for ii = 1:It %summation over a-arrays
abc = abc + a(ii)
end
abc %final array with summed elements
What is wrong with it?

7 Comments

I think I have the error: with the first for-iteration I asign the vector [1 1 rand()] to the first matrix element a(1), the second iteration asigns vector [1 1 rand()] to the matrix element a(2) etc. So a(ii) is the syntax of the matrix element ii of the matrix "a". Do I see this right?
But what i want to do is to do some indexing 1 to 3 for a whole matrix with name "a" without having to give the matrix "a" of the first iteration, the second, and the third a different name like "a", "b", and "c". I need this indexing for a whole matrix because in the second for-loop I want to sum over all the different matrices, starting with matrix abc with dimension of the other matrices and adding the three matrices from above in each iteration. How can this be done?
When you have
a(ii) = something
and ii is a scalar value, then the "something" must be exactly one element long. You cannot store a vector as a matrix element. The closest you can get to that is to create a cell from the vector, and then store the cell in the array:
a(ii) = {[1 1 rand()]};
stores the single cell into a(ii). Another way of writing the same thing is
a{ii} = [1 1 rand()];
Notice the use of {} instead of (). You can refer to the content as a{ii} in statements such as
abc = abc + a{ii};
However... your code would be a lot more efficient if you stored everything in a big matrix and used sum() over the appropriate dimension of the matrix.
for ii = 1 : 3
aa(ii,:) = [1 1 rand()];
end
sum(aa,1) %adds the three rows
Thank you for these clarifications. As my real code with fourier calculations and matrices with random elements it only work with matrices as large as about 2048. For some reason that nobody could figure out...but it works...we cannot go larger. So I will try a{ii} or the other one.
Ok. Both give error messages. I will post the code and explain. The 3 matrices I am reading in at line 10,11,12 have the format 1957x1957x3 uint8. The file test is mainly a loop through another file where these matrices are worked with and are given out at the end - so they are not overwritten afterwards. The images (of the matrices) can be improved by stacking several of them and doing an average. SO I need to read out at least two times these matrices give them a different name OR an index through which I can go in a for-loop when stacking the matrices and plotting them as a figure in the end. I hope youunde rstand what I mean.
When enough memory exists, to calculate sum() or mean() or std() or var() or the like, cat() on a dimension one higher than the number of dimensions of the array, and then apply the function along that same dimension number. For example,
mean( cat(4, A1, A2, A3, A4, A5), 4)
Likewise:
numfile = 7;
imgs = zeros(1957, 1957, 3, numfile);
for K = 1 : numfile
imgs(:,:,:,K) = imread(sprintf('image_%04d.jpg', K));
end
meanimg = mean(imgs,4);
This seems to work in the sense that the programm goes into both of my for-loops. Can you tell me why the imgs in your example has the format 4-D uint8 instead of being 3D? That gave me the error message that "Integers can only be combined with integers of the same class, or scalar doubles." What I try now is putting your imgs into double(imgs). Any recommendations?
Lucius
Lucius on 24 May 2015
Edited: Lucius on 24 May 2015
-

Sign in to comment.

Products

Asked:

on 23 May 2015

Edited:

on 24 May 2015

Community Treasure Hunt

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

Start Hunting!