create a structure from a matrix

1 vue (au cours des 30 derniers jours)
leah biessenberger
leah biessenberger le 3 Fév 2017
I have an nx2 matrix. The second column is meant to provide indexes into the first column: ex. please ignore the given x, it is there so the matrix below would look like I wanted it too.
x=[1;0.5]
0.5 1
0.75 1
1.0 1
1.25 1
0.2 2
0.4 2
0.6 2
0.15 3
0.3 3
0.45 3
I am trying to create a for loop to create a structure where each of the values in column 1 are put into its own structure based on the value in column 2. What I have so far:
trialdata=load(file);
numb=trialdata(:,2);
cell.numb=num2cell(MUnumb);
DTs=trialdata(:,1);
cell.DTs=num2cell(DTs);
DTs_per_numb=struct(field,trialdata);
fields='number';
for i={1:max(numb)};
DTs_per_numb.(fields{i})=cell.DTs(cell.numb==i);
end
I am getting the following error:
Cell contents reference from a non-cell array object.
Then need to iterate through each part of structure to run stats (ex. mean) Would appreciate any help. Thanks

Réponse acceptée

Stephen23
Stephen23 le 3 Fév 2017
Modifié(e) : Stephen23 le 3 Fév 2017
Lets have a look:
fields='number';
defines a 1xN character array. This is okay. But
for i={1:max(numb)};
likely does not do what you intended: 1:max(numb) generates a vector of numbers (ok), but doing {1:max(numb)} puts it into a cell array (note the brackets {} !).
Once you put that vector into one cell of a cell array, then your for loop will iterate exactly once (for that one cell), and the loop variable will be the entirety of that vector all at once. Most likely you wanted this:
for k = 1:max(numb)
which will iterate over the values in that vector. The line inside does not make sense either. You defined fields as one 1xN char array, so why do you now try to access it using cell array indexing: fields{i} ? fields is not a cell array.
You need to learn what cell arrays are, and how to index into them:
  3 commentaires
Stephen23
Stephen23 le 3 Fév 2017
Modifié(e) : Stephen23 le 3 Fév 2017
@leah biessenberger: I apologize if you feel insulted: actually I come here to try to get peoples code working. So lets give it a try. When you write
fields='number';
then you told us that fields is a string. Now it occurred to me that perhaps you intend fields to be a number value? When you clarify your intent then we can see how to make it work. Your code concept seems fine, it is just not at all clear from your code or explanation what you actually want to be looping over.
Here is an alternative way without a loop:
x = [1,0.5;
0.5 1;
0.75 1;
1.0 1;
1.25 1;
0.2 2;
0.4 2;
0.6 2;
0.15 3;
0.3 3;
0.45 3]
[~,~,idx] = unique(x(:,2))
C = accumarray(idx,x(:,1),[],@(n){n})
leah biessenberger
leah biessenberger le 3 Fév 2017
That worked beautifully. Thank you so very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures 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