How to extract columns of a matrix into seperate vectors?
1.762 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Oleg Komarov
il 7 Set 2012
Modificato: Oleg Komarov
il 7 Set 2012
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.
Risposte (4)
Image Analyst
il 7 Set 2012
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 Commenti
Stephen23
il 21 Nov 2017
Modificato: Stephen23
il 21 Nov 2017
@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
il 25 Nov 2017
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
il 7 Set 2012
x = mat(:,1);
y = mat(:,2);
%etc.
But like Oleg said - don't do it!
0 Commenti
Korosh Agha Mohammad Ghasemi
il 25 Giu 2024
Spostato: Voss
il 25 Giu 2024
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 Commento
Voss
il 25 Giu 2024
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)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!