how do i take the data from cells in a cell array and turn them into a data array

3 vues (au cours des 30 derniers jours)
i am writing a data anylsis script where I import an excell file, sort the data in that file by the first column then seperate out all the rows that have the same value into their out matrices. So far i have been able to sort all rows that have the same values in column one into individul cells in a cell array
[u,~,ib] = unique(T1(:,1));
[M,~] = size(u);
C = cell(1,M);
clear M
for i = 1:length(u)
C(i) = {T1(ib==i,:)};
end
[~,N] = size(C); % how many cells are in the array
Now i want to extract those values out of the cells into individual data arrays but i dont know how many arrays i will need.
i know that to convert a cell array to a data matrix i would use
Ai = cell2mat(C{i})
i mainly want to know how to create as many "A" matrices as required with out knowing how large "N" is before hand
  4 commentaires
madhan ravi
madhan ravi le 31 Jan 2019
if you want to store them in separate matrices use 3D matrix but don’t think of naming variables dynamically!
Stephen23
Stephen23 le 31 Jan 2019
Simpler:
[u,~,ib] = unique(T1(:,1));
N = numel(u);
C = cell(1,N);
for k = 1:N
C{k} = T1(ib==k,:);
end
or even using accumarray. The simplest and most effiicient way to store and access those matrices is to use a cell array. Naming variables dynamically is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:

Connectez-vous pour commenter.

Réponses (1)

Jos (10584)
Jos (10584) le 31 Jan 2019
This is the accumarray solution that Stephen hinted to:
T1 = [99 10 11 ; 99 20 21 ; 30 31 32]
[~,~,ib] = unique(T1(:,1), 'stable')
C = accumarray(ib, 1:size(T1,1), [], @(k) {T1(k,:)})

Catégories

En savoir plus sur Cell Arrays 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!

Translated by