# Thread: Matlab - Storing a row for a matrix?

1. ## Matlab - Storing a row for a matrix?

Hey all, I've got a little question, Hoping I can make myself clear.

I'm building a 'For' function X times (Inside a while loop) in order to sum it all up to a matrix (every 'for' loop is one row in the matrix).
The problem - Everytime It's running the 'for' function, it's deleting the previous for function it created (It's quite obvious, cuz i'm using the same variable).
Is there any function to store every run of the 'for' function in a new variable in order to sum it all up in the end ?

Hopes it's clear.
Thanks.

2. Originally Posted by Geomorphologist
Hey all, I've got a little question, Hoping I can make myself clear.

I'm building a 'For' function X times (Inside a while loop) in order to sum it all up to a matrix (every 'for' loop is one row in the matrix).
The problem - Everytime It's running the 'for' function, it's deleting the previous for function it created (It's quite obvious, cuz i'm using the same variable).
Is there any function to store every run of the 'for' function in a new variable in order to sum it all up in the end ?

Hopes it's clear.
Thanks.
Create an array to hold the results and store them in the array as you compute them.

CB

3. Originally Posted by CaptainBlack
Create an array to hold the results and store them in the array as you compute them.

CB
Is there a special way to do that ? If i'm creating a matrix : 'matrix = [a]' It will still replace the 'a' every time. Am I missing something ?

4. Originally Posted by Geomorphologist
Is there a special way to do that ? If i'm creating a matrix : 'matrix = [a]' It will still replace the 'a' every time. Am I missing something ?
Code:
matrix=zeros(1,10);
for i=1:10

.. do calculations for a

matrix[i]=a;
end
CB

5. Originally Posted by CaptainBlack
Code:
matrix=zeros(1,10);
for i=1:10

.. do calculations for a

matrix[i]=a;
end
CB
I really appreciate your help, but still - I doesn't work
There's the code I created , It's working fine, but not storing every input like I stated before (the code is without your addons):

Code:
c = input('Please enter number of columns in Matrix: ');
d = input('Please enter number of rows in Matrix: ');
e=d;
while (e > 0)
e=e-1;
for b = 1:1:c
a(b) = input('Enter a number for the matrix: ');
end

end
Can you be more specific ?
Thank you very very very much!

6. Originally Posted by Geomorphologist
I really appreciate your help, but still - I doesn't work
There's the code I created , It's working fine, but not storing every input like I stated before (the code is without your addons):

Code:
c = input('Please enter number of columns in Matrix: ');
d = input('Please enter number of rows in Matrix: ');
e=d;
while (e > 0)
e=e-1;
for b = 1:1:c
a(b) = input('Enter a number for the matrix: ');
end

end
Can you be more specific ?
Thank you very very very much!
Code:
c = input('Please enter number of columns in Matrix: ');
d = input('Please enter number of rows in Matrix: ');
e=d;

a=zeros(c,d)

for e=d:-1:1

for b = 1:1:c
a(b,e) = input('Enter a number for the matrix: ');
end

end

7. Thank you very much, working like a charm (I flipped the ' a(b,e) = input' in order for it to work properly).
Thank you very much, I'm quite new in Matlab and I see I complicated it too much with the While loop.