How to extract columns of a matrix into seperate vectors?
1,699 ビュー (過去 30 日間)
古いコメントを表示
Hallo,
Let's assume a matrix (100x3). I want to extract out each of the columns, and store them in 3 separate vectors.
The code for the loop is clear, but what's the right code for storing each column?
----------------------------
for i = 1 : 3
vector = matrix(:, i);
end
----------------------------
I guess it's probably simple, but I am still a beginner and couldn't find much useful information in the documentation so far.
Thank you. Dominik
1 件のコメント
Oleg Komarov
2012 年 9 月 7 日
編集済み: Oleg Komarov
2012 年 9 月 7 日
Why do you want to do that? And, don't do that. Storing your data has to be kept separate from the design you have in mind.
回答 (4 件)
Image Analyst
2012 年 9 月 7 日
Try this:
column1 = your2DMatrix(:, 1);
column2 = your2DMatrix(:, 2);
column3 = your2DMatrix(:, 3);
Unlike the others, I don't find any problem with doing this if referencing these 3 column vectors individually by name will make your code easier to understand and follow. It will certainly make your subsequent code more compact. I wouldn't do it if you had dozens and dozens of columns, but for only 3 I don't see a problem.
6 件のコメント
Stephen23
2017 年 11 月 21 日
編集済み: Stephen23
2017 年 11 月 21 日
@Doina Gumeniuc: if you "went through this conversation" then you must have read the comments where very experienced MATLAB users and TMW staff recommend not doing this for a larger number of vectors.
Read this to know why they all agreed that this is a bad idea:
"so taking it one by one is a bit painful."
Yes, it is. And it is almost invariably a slow, buggy, complex way to write code, no matter how many beginners want to do this. What do you hope to achieve with 50 vectors in your workspace? How do you imagine passing 50 variables to SIN, or any other function? How would you search for those variables in the editor?
Doina Gumeniuc
2017 年 11 月 25 日
Hi Stephen, thank you for your reply, my bad for not noticing the information you mentioned. It is nice to have the possibility to ask for advice the more experienced matlab users. I have some operations to do column by column, I wrote a post for it, please see it here https://se.mathworks.com/matlabcentral/answers/368884-operation-with-big-files
Thank you!
Sean de Wolski
2012 年 9 月 7 日
x = mat(:,1);
y = mat(:,2);
%etc.
But like Oleg said - don't do it!
0 件のコメント
Korosh Agha Mohammad Ghasemi
2024 年 6 月 25 日
移動済み: Voss
2024 年 6 月 25 日
To extract each column of a matrix and store them in separate vectors, you can use a loop in MATLAB. You can store each vector in a cell array or directly assign them to separate variables if you know the number of columns in advance. Below are two methods to achieve this.Method 1: Using a Cell Array
This method is useful if the number of columns is not known in advance or if you prefer dynamic storage.
% Example matrix (100x3)
matrix = rand(100, 3);
% Preallocate a cell array to store each column
numCols = size(matrix, 2);
vectors = cell(1, numCols);
% Loop to extract each column and store it in the cell array
for i = 1:numCols
vectors{i} = matrix(:, i);
end
% Display the vectors
for i = 1:numCols
fprintf('Vector %d:\n', i);
disp(vectors{i});
end
Method 2: Assigning to Separate Variables
This method is suitable if you know the number of columns beforehand and prefer direct variable assignment.
% Example matrix (100x3)
matrix = rand(100, 3);
% Preallocate vectors (if necessary)
vector1 = matrix(:, 1);
vector2 = matrix(:, 2);
vector3 = matrix(:, 3);
% Display the vectors
disp('Vector 1:');
disp(vector1);
disp('Vector 2:');
disp(vector2);
disp('Vector 3:');
disp(vector3);
Loop Method with Dynamic Variable Names (Not Recommended)
Using dynamic variable names in a loop is generally discouraged because it makes the code harder to read and maintain. However, for completeness, here’s how you might do it:
% Example matrix (100x3)
matrix = rand(100, 3);
% Loop to extract each column and assign to dynamically named variables
for i = 1:3
eval(sprintf('vector%d = matrix(:, i);', i));
end
% Display the vectors
disp('Vector 1:');
disp(vector1);
disp('Vector 2:');
disp(vector2);
disp('Vector 3:');
disp(vector3);
Summary
- Cell Array Method: Flexible and scalable for an unknown number of columns.
- Direct Assignment: Simple and straightforward for a known number of columns.
- Dynamic Variable Names: Generally not recommended due to readability and maintainability issues.
Choose the method that best suits your needs and coding style.
1 件のコメント
Voss
2024 年 6 月 25 日
An alternative method using a cell array is to use num2cell with the dimension argument specified.
Example:
matrix = rand(100, 3);
vectors = num2cell(matrix,1)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!