How to access an individual column from data

Hi guys, this might be a simple question but i don't really know how to do it.
I have 10x8 matrix, and want to specify every column besides the first one as: x1,x2,x3,x4,x5,x6,x7. Like, x1 = [...], x2 =[...] etc. Until now i've done it manually by copy-paste the values, but i'm sure there is a much easier way.
Thanks, B!

2 件のコメント

Stephen23
Stephen23 2017 年 11 月 28 日
"...i'm sure there is a much easier way."
Yes, there is: use indexing. Indexing is simple, neat, and very efficient. Do NOT magically create or access variable names in a loop:
Betty
Betty 2017 年 11 月 28 日
Can you give me an example of what will be easiest in my way?

サインインしてコメントする。

回答 (2 件)

KSSV
KSSV 2017 年 11 月 28 日

3 投票

It is not required to name each column by x1, x2....x10.....it is waste of time and not all necessary. Already you have all the data in a matrix form..this is sufficient.
A = rand(10,8) ; % matrix
A(:,1) % gives first column
A(:,7) % gives seventh column
A(:,end) % gives last column ie eight

5 件のコメント

Jan
Jan 2017 年 11 月 28 日
編集済み: Jan 2017 年 11 月 28 日
@Betty: See also: FAQ: Create variables A1, A2, ... and Tutorial: Don't EVAL . The dynamic creation of a set of variables is a bad programming style and produces more problems than it solves. Trust KSSV's suggestion. +1
Betty
Betty 2017 年 11 月 28 日
Thank you. The reason i want to specify it as x's is because i want to use them to make a multiple regression afterwards. I know this i time consuming but my coding skills are not the best:)
KSSV
KSSV 2017 年 11 月 28 日
Very much you can use....put a loop...
A = rand(10,8) ; % matrix
for i = 1:size(A,2)
coli = A(:,i) ;
% do what you want
end
Betty
Betty 2017 年 11 月 28 日
Why do you rand my matrix?
Stephen23
Stephen23 2017 年 11 月 28 日
編集済み: Stephen23 2017 年 11 月 28 日
@Betty: KSSV did not "rand" your matrix. KSSV used rand as a substitute for the data that you did not give us. Without any data we cannot test code... so KSSV quite sensibly made some fake data using rand.

サインインしてコメントする。

Stephen23
Stephen23 2017 年 11 月 28 日
編集済み: Stephen23 2017 年 11 月 28 日

0 投票

If mat is your matrix, then something like this:
mat = [...];
out = [...];
for k = 1:size(M,2)
vec = mat(:,k);
... do whatever with vec
out(k) = ... save the output
end
You will need to figure out the require size of the output matrix.
Note that basic MATLAB concepts, such as how to write loops and access data using indexing, are introduced in the introductory tutorials. These tutorials are highly recommended for all beginners:

カテゴリ

ヘルプ センター および File ExchangeSpline Postprocessing についてさらに検索

質問済み:

2017 年 11 月 28 日

編集済み:

2017 年 11 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by