# Extracting data from 2 column array based on infromation in the 1st colum

1 Ansicht (letzte 30 Tage)
Jonathan Duncan am 3 Okt. 2016
Kommentiert: Jonathan Duncan am 7 Okt. 2016
Hello first time posting, I'm having a bit of trouble extracting information from a 2 column array here is a an example array. There is an attach example of some an array, what I've been trying to do is find a way to sort out all the data in the 2nd column the corresponds to 1, 2 etc, and create corrosponding vectors for each set of data. here is some of the code I've been messing with
>> for i = if B(1:end,1 == 0) A = B
end end
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Stephen23 am 3 Okt. 2016
Bearbeitet: Stephen23 am 3 Okt. 2016
The best solution is to use accumarray:
>> A = [1,36;1,24;1,26;1,82;1,45;,2,68;2,27;2,91;3,91]
A =
1 36
1 24
1 26
1 82
1 45
2 68
2 27
2 91
3 91
>> Z = accumarray(A(:,1),A(:,2),[],@(n){n})
Z =
[5x1 double]
[3x1 double]
[ 91]
This puts the data into a cell array, using the values in the first column as indices. You can access the data in the cell array using cell array indexing:
>> Z{1}
ans =
36
24
26
82
45
>> Z{2}
ans =
68
27
91
>> Z{3}
ans =
91
##### 1 KommentarKeine anzeigenKeine ausblenden
Jonathan Duncan am 7 Okt. 2016
This works very well thanks for the help

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Matrices and 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!

Translated by