# How do I extract specific data from an array inside a cell array using indexing?

24 Ansichten (letzte 30 Tage)
Joel Peeters am 14 Apr. 2020
Bearbeitet: Ameer Hamza am 14 Apr. 2020
So I have a cell array B of size 4*101 in which each cell contains an array of size 1*3, like this:
B = cell([4 101]);
for i = 1:4
for ii = 1:101
B{i,ii} = rand([1 3]);
end
end
And I need to get the third element of the 1*3 arrays from all cells in the top row of the cell array. I can do that using
C = zeros(101,1)
for i = 1:101
C(i) = B{1,i}(3);
end
But since I need to extract this kind of data quite a lot it'd be a lot nicer to be able to do this in one line using indexing. I figured using
C = B{1,:}(3);
Should work but this gives the error
"Expected one output from a curly brace or dot indexing expression, but there were 101 results."
How do I index this so that it works?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Mehmed Saad am 14 Apr. 2020
use cellfun
cellfun(@(x) x(3),B(1,:))
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

Peng Li am 14 Apr. 2020
d = cellfun(@(x) x(3), B);
d gives you the third element from all cells. You can index into d to find those that correspond to the first row.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Mehmed Saad am 14 Apr. 2020

Melden Sie sich an, um zu kommentieren.

Ameer Hamza am 14 Apr. 2020
Bearbeitet: Ameer Hamza am 14 Apr. 2020
Use can use basic indexing operations in MATLAB without the need to call any function
B = cell([4 101]);
for i = 1:4
for ii = 1:101
B{i,ii} = rand([1 3]);
end
end
result = [B{1,:}];
result = result(3:3:end);
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

R2020a

### Community Treasure Hunt

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

Start Hunting!