Data splitting and saving to different variables
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
How to split a matrix data column wise and saving each column in different variable using loop. say M is a 3*8 matrix. I want to store these data of M columnwise into a 8 different column matrix, m1,m2,m3,m4,m5,m6,m7 and m8.
2 commentaires
Stephen23
le 4 Juil 2022
"I want to store these data of M columnwise into a 8 different column matrix, m1,m2,m3,m4,m5,m6,m7 and m8."
You can certainly do that, if you want to force yourself into writing slow, complex, inefficient, obfuscated, insecure, buggy code that is hard to debug:
In contrast, indexing is simple and very efficient. So far you have not given any reason why you cannot use indexing.
Réponses (2)
Johan
le 4 Juil 2022
You can use structure to do that, you can read more about it in the matlab help https://fr.mathworks.com/help/matlab/ref/struct.html
here is an example for your specific question:
M = rand(3,8)
m(1:8) = struct('data',[]);
for i_col = 1:size(M,2)
m(i_col).data = M(:,i_col);
end
m.data
% Note that splitting that little data in structured arrays is not very
% efficient
whos M
whos m
3 commentaires
dpb
le 4 Juil 2022
And, for good measure, with the table, OP gets the desired variable names coming along "for free"...
Steven Lord
le 4 Juil 2022
Can you dynamically create variables with numbered names like x1, x2, x3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!