Extract specific rows of a cell

26 次查看(过去 30 天)
Majid Mostafavi
Majid Mostafavi 2020-1-25
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 个评论
Akira Agata
Akira Agata 2020-1-25
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 2020-1-25
yes
but A{2}(4) give me only second element of first column

请先登录,再进行评论。

回答(1 个)

Akira Agata
Akira Agata 2020-1-25
编辑:Akira Agata 2020-1-25
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!

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by