Extract specific rows of a cell

16 views (last 30 days)
Majid Mostafavi
Majid Mostafavi on 25 Jan 2020
Edited: Akira Agata on 25 Jan 2020
Hey everyone
I have a cell A as below and want to extract a matrix form A of specific rows which stored at r from each rows of A for example row number 4 from first row of A and ...
A= { 90×1 double} { 90×1 double} { 90×1 double}
{101×1 double} {101×1 double} {101×1 double}
{101×1 double} {101×1 double} {101×1 double}
{100×1 double} {100×1 double} {100×1 double}
{ 97×1 double} { 97×1 double} { 97×1 double}
{ 75×1 double} { 75×1 double} { 75×1 double}
r=
4
6
99
43
68
50
  2 Comments
Akira Agata
Akira Agata on 25 Jan 2020
Question for clarification.
Is the cell array A a 2-D (N-by-M) ? or 1-D (1-by-N or N-by-1) ?
If A is a 1-D cell array, you want to extract k-th number from each double array stored in a cell?
For example, if r = 4, you want to extract A{1}(4), A{2}(4), ..., A{N}(4) and make a 1-D double array [A{1}(4), A{2}(4), ..., A{N}(4)] ?
Majid Mostafavi
Majid Mostafavi on 25 Jan 2020
yes
but A{2}(4) give me only second element of first column

Sign in to comment.

Answers (1)

Akira Agata
Akira Agata on 25 Jan 2020
Edited: Akira Agata on 25 Jan 2020
OK. Then, to avoid misunderstanding, let's use a simple example.
Say, A is a 1-by-3 cell array and r = 4, as follows:
A = {rand(90,1), rand(101,1), rand(100,1)};
r = 4;
If you write [A{1}(r), A{2}(r), A{3}(r)], then you can extract the 4th element of each cell.
output = [A{1}(r), A{2}(r), A{3}(r)];
But if A is large array, such as 1-by-10000, it's impossible to use the above solution.
In such a case, I would recommend using cellfun function to do the same thing, like this:
output = cellfun(@(x) x(r), A);
I hope this is answering to your question!

Categories

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