Create sperate matrices for all unique values in the 3rd column of a cell array
4 views (last 30 days)
Show older comments
I have a cell array of 666x3, in this array the first and second columns have completely unique values, but the 3rd column has identifying values. I want to make a new array for each identifying value. Keep in mind there are many different values in column 3 as well and i dont want to manually input them, I want it to be done automatically so any data I use, the same result will be shown. Thank you!
Example:
initialArray =
column1 column2 column3
240.747574230792 298.790970549445 127
239.818737359994 297.862133678646 127
241.676411101591 297.862133678646 126
240.283155795393 296.933296807848 126
235.174553006003 296.004459937050 125
233.781297699806 295.540041501651 125
220.777581508630 269.997027554699 125
required:
array1=
column1 column2 column3
240.747574230792 298.790970549445 127
239.818737359994 297.862133678646 127
array2=
column1 column2 column3
241.676411101591 297.862133678646 126
240.283155795393 296.933296807848 126
array3=
column1 column2 column3
235.174553006003 296.004459937050 125
233.781297699806 295.540041501651 125
220.777581508630 269.997027554699 125
0 Comments
Accepted Answer
Matt J
on 2 Jun 2021
Edited: Matt J
on 2 Jun 2021
Well, I won't talk about creating separate arrays because it's bad, but here's what you could do which wouldn't be bad:
initialArray = {
240.747574230792 298.790970549445 127
239.818737359994 297.862133678646 127
241.676411101591 297.862133678646 126
240.283155795393 296.933296807848 126
235.174553006003 296.004459937050 125
233.781297699806 295.540041501651 125
220.777581508630 269.997027554699 125};
initialArray=cell2mat(initialArray);
[~,~,G]=unique(initialArray(:,3),'stable');
array = splitapply(@(x){x}, initialArray, G);
It's essentially what you asked for, except array is a cell array whose contents are the array chunks that you asked for.
array{:}
3 Comments
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!