How to create sequences of vector from a matrix's columns?

Lets say we have a matrix:
data= [240 7 0.0070 100
200 10 0.0095 50
220 8.5 0.0090 80 ];
To, create vectors from each columns we usually write
P1 = data(:,1);
P2 = data(:,2);
P3 = data(:,3);
P4 = data(:,4);
Is it possible initialize P1, P2, P3, and P4 with a loop? I have hundred columns of matrix needed to be vectorized. Thanks everyone.

 Accepted Answer

While I agree mostly with Dr. Siva Srinivas Kolukula's answer, a compromise is to convert to a cell array,
P=num2cell(data,1);
and access the columns as P{1}, P{2}, etc... This has some advantages, since repeatedly accessing columns using the syntax data(:,i) will trigger repeated memory allocations for the extracted column vectors (at least in some versions of MATLAB). Using cell arrays avoids this.

More Answers (1)

Why you want to name them p1,p2,p3 etc when already yu can access them by data(:,1),data(:,2)etc. Naming variable the way you want is called dynamic variable naming. This is not suggested.
More on

Community Treasure Hunt

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

Start Hunting!