# MATLAB Basics video: Storing data in a matrix from a loop 58

Posted by **Doug Hull**,

I was recently answering a question that came in from a MATLAB user on this blog. In the end, the question was really one where he was doing a calculation and storing the result in scalar Y. When he was done with the loop, he only had the final value of Y but not all of them that he had calculated on the way. The solution to this was to store each value of Y into a vector. This video is meant for new users to show how to do that.

Also note, that I have been reformatting these videos and tagging them as videos. If you click on videos under categories to the right, you can see all the videos in one place.

Other videos have been gathered here:

https://blogs.mathworks.com/pick/category/video/

Other MATLAB Basics posts have been gathered here:

https://blogs.mathworks.com/pick/category/matlab-basics/

**Category:**- Format: Video,
- Level: Basic

## 58 CommentsOldest to Newest

**1**of 58

**2**of 58

**3**of 58

**4**of 58

**5**of 58

```
fileName = 'a.xls';
range = 'A1:A500';
x = cell(1, 39); % Preallocation
for k = 1:39
x{k} = xlsread(fileName, k, range);
end
```

The data from the kth sheet will be in x{k} (note the curly braces instead of parentheses.)
JJ Garza,
If you want to run your iterations from -10 to 10 and store all the data, I can think of two easy ways to do so. Method 1 involves transforming your FOR loop counter to use as indices:
```
x = zeros(1, 21); % Preallocation
for k = -10:10
% since we want to store k = -10 in element 1 of x
% we need to add 11 to k to make it the correct index
x(k+11) = k.^2;
end
```

Method 2 is to use the FOR loop index not as the actual iterates, but to index into another vector. For example:
```
y = -10:10;
x2 = zeros(size(y)); % Preallocation
for k = 1:length(y)
x2(k) = y(k).^2;
end
```

The latter scenario is useful when it would be difficult or impossible to find a way to map the iterations you want to do to the indices where you want to store the result. For instance:
```
y = [2 3 5 7 11 13 17 19 23];
x = false(size(y));
for k = 1:length(y)
x(k) = isprime(y(k));
end
```

It would be hard to convert the elements of y into [1, 2, 3, ...] to use as indices. **6**of 58

**7**of 58

**8**of 58

**9**of 58

**10**of 58

**11**of 58

**12**of 58

**13**of 58

**14**of 58

**15**of 58

**16**of 58

**17**of 58

**18**of 58

**19**of 58

**20**of 58

**21**of 58

**22**of 58

```
v = -c:c/10:c;
T = zeros(size(v)); % Preallocation
for k = 1:numel(v)
T(k) = equation(v(k));
end
```

The preallocation step is very important for performance, so that T doesn't grow (and need to be reallocated) each time through the loop. **23**of 58

**24**of 58

H20 = fspecial('gaussian',[100 100],20); lowp20 = imfilter(data,H20,'replicate'); H21 = fspecial('gaussian',[105 105],21); lowp21 = imfilter(data,H21,'replicate'); ... ... H60 = fspecial('gaussian',[300 300],60); lowp60 = imfilter(data,H60,'replicate');and then I want to compare each result to the original, say

mse20=mean((lowp20(:)-data(:)).^2); mse21=mean((lowp21(:)-data(:)).^2); ... ... mse60=mean((lowp60(:)-data(:)).^2);You can see where this is going, tons of typing. What I would like to be able to do is something like

for i=20:60 H(i) = fspecial('gaussian',[5i 5i],i); lowp(i) = imfilter(data,H(i),'replicate'); endBut I am stuck with the wrong syntax and I also suspect I should create a cell array to store the resulting 41 matrices of filtered data? Any help would be great. Thank you

**25**of 58

**26**of 58

**27**of 58

**28**of 58

m = 5; for (i = 1 :1:m) p = input( 'enter the species no:' ) ; n=10; x = []; for k=1:1:n a = input('enter the subgroup no'); b = input('enter the quantity of that subgroup present '); if ( a==0) % Type subgroup no (a) = 0 to stop entry for this compound % break; end; y = [a b]; x = [x ; y]; end; x end;

**29**of 58

m = 5; for (i = 1 :1:m) p = 1 n=10; x = []; for k=1:1:n a = 1 b = 1 if ( a==0) % Type subgroup no (a) = 0 to stop entry for this compound % break; end; y = [a b]; x(:,:,k) = y; end; x end;

**30**of 58

**31**of 58

**32**of 58

**33**of 58

**34**of 58

**35**of 58

m = 8 1 6 3 5 7 4 9 2 >> r = find((m(:,1) == 8) & (m(:,2) < 3)) r = 1

**36**of 58

**37**of 58

a = [1 2 3] b = [4 5 6] c = [7 8 9] M(1,:) = a M(2,:) = b M(3,:) = c

**38**of 58

**39**of 58

**40**of 58

**41**of 58

**42**of 58

**43**of 58

**44**of 58

**45**of 58

**46**of 58

**47**of 58

**48**of 58

**49**of 58

**50**of 58

**51**of 58

**52**of 58

**53**of 58

**54**of 58

**55**of 58

**56**of 58

**57**of 58

**58**of 58

## Recent Comments