Azzera filtri
Azzera filtri

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

4 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Matt J
Matt J il 2 Giu 2021
Modificato: Matt J il 2 Giu 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 Commenti
Talha Majeed
Talha Majeed il 2 Giu 2021
You're right it does, I was looking at the wrong thing, thanks for you help!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Tag

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by