필터 지우기
필터 지우기

Extract specific rows of a cell

조회 수: 19 (최근 30일)
Majid Mostafavi
Majid Mostafavi 2020년 1월 25일
편집: Akira Agata 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