Clear Filters
Clear Filters

Create sperate matrices for all unique values in the 3rd column of a cell array

4 views (last 30 days)
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

Accepted Answer

Matt J
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{:}
ans = 2×3
240.7476 298.7910 127.0000 239.8187 297.8621 127.0000
ans = 2×3
241.6764 297.8621 126.0000 240.2832 296.9333 126.0000
ans = 3×3
235.1746 296.0045 125.0000 233.7813 295.5400 125.0000 220.7776 269.9970 125.0000
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!