How to store a data by getting from for loop in matlab ?

For example i'm getting value like this
a=[192.1225 60.2626 0.9993 100.3275];
for 'n' number of iteration, which means i will get so many values like
a=[192.1225 60.2626 0.9993 100.3275];
a=[20.1225 66.2626 1.0 10.8775];
a=[2.0925 64.2626 1.0 110.5475]; .....
it will continues like this and it will stop based on the 'for loop'
and now i want to store all the 'a' values in 'b' like this
b=[192.1225 60.2626 0.9993 100.3275
20.1225 66.2626 1.0 10.8775
2.0925 64.2626 1.0 110.5475];
and again the same value should display like this
p=[192.1225 60.2626 0.9993 100.3275];
q=[20.1225 66.2626 1.0 10.8775];
r=[2.0925 64.2626 1.0 110.5475]; .....

Answers (2)

If you know how many columns a is going to have and how many rows your final result will have (presumably you do if it is in a for loop) then you don't need the intermediate 'a' result at all.
Just pre-declare b as e.g
b = zeros( n, 4 );
for i = 1:n
b( i, : ) = yourCalculation();
end
where n is obviously however many rows you intends to have.

9 Comments

sir, i'm getting wrong answer please refer my above Attached file "code_for_mathwork.txt"
My for loop is the same for loop you already have in your code, not one to go after it. You can collect all your answers into one matrix, b (but give it a sensible name!) during the for loop rather than after it. That is what my code does.
You have to substitute in your numbers and code though, 'n' is whatever condition you currently have on your for loop.
ya but i getting only last values...
You should be getting everything. i increments down each row of 'b' as you go through your for loop so each row of b should contain the result from that iteration.
sir, i'm getting like this, sir my attached file that i modified in my code, at last part you can see...
b=[ 0 0 0 0
0 0 0 0
184.8580 82.6179 0.9995 86.6199]
You have:
b = [];
at the top which won't work for this method. You have to presize b as I showed above
sir, i tried with that but i dint get i'm getting same result .
Your
b( i, : ) = CH;
instruction appears to be in an inner loop defined by k and an outer loop defined by i. So you will lose all the results except the last one from your inner k-controlled loop and only store the last result for each iteration of the outer loop.
I would strongly advise factoring your code out into functions though to make it more readable as one huge file with loads of nested for loops and instructions is hard to understand
+1 for starting the code with array-preallocation.
Orion
Orion on 24 Nov 2014
Edited: Orion on 24 Nov 2014
it's basic concatenation
b=[];
for i=1:n
% some calculation
a = % your new row vector
b=[b;a];
end
then you get a matrix b, and you just have to rerieve the line that interests you.
l = b(2,:);
...

6 Comments

no, i'm getting wrong answer... i tried your code i'm like this
a=[192.1225 60.2626 0.9993 100.3275
192.1225 60.2626 0.9993 100.3275
192.1225 60.2626 0.9993 100.3275
192.1225 60.2626 0.9993 100.3275
192.1225 60.2626 0.9993 100.3275].....
I just gave you the method. I don't know what are your data and how you create them.
What is your code so far ?
If your a changes in the loop, as you said, than Orions solution
b = [b;a];
simply cannot result in all the same values for each row of b, if put in your loop. Please check.
my code's are some what big so i'm Attaching the file please check
In this i was highlighting my problem with this '%%%%%%%%%%%' at last of my u can find.
In your code, you put
b=[];
for i=1:n
% some calculation
a = CH;
b=[b;a];
b
end
You are concatenating n times a with himself in b. it's useless.
You need to add a new row in b, only if there is a new calculation.
See the 2 modif I made in attached file (search %# Modification).
b is initialized at the beginning, and increases only where a new CH is calculated.
I don't know if this is the result you want. bu this is the methid you need to do.
You just might change the condition for the concatenation (or the location in the code), but this only depends on what you want to do.
ya. but sir, you just run the code once again and type capital 'X' in command window and similarly Capital 'Y' after executing that you will get the position on 'Cluster head (CH)' so at final i want only those values with energy and distance.
For example
CH=[164.9445 90.7746 0.9996 65.5964]
in this first two values represents 'X' and 'Y' and remaining are energy and distance.

This question is closed.

Asked:

on 24 Nov 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!