How to get ( number and values) of (non-empty elements) in each column of the matrix??

조회 수: 35 (최근 30일)
Hi all, I have the following matrix;
P =
Columns 1 through 2
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x2 double] [2x2 double]
[2x2 double] [2x2 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
Columns 3 through 4
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x2 double]
[2x2 double] [2x2 double]
[2x2 double] [2x2 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
I want to get the number of the non-empty elements in each column and then their values.
For example; in Column 1, there are two non-empty elements [2x2 double]. To get their values; I should write like that; P{row_no,1} ;
>> P{7,1}
ans =
0.5878 0.71222
0 0
>> P{8,1}
ans =
0.78307 1.117
0 0
So, My question is; how to get the number of the non-empty elements in each column and also how to get the values of these non-empty elements?
  댓글 수: 1
Image Analyst
Image Analyst 2020년 2월 1일
Original question from Erman (in case he decides to delete this one also):
How to get (the number and the values) of the (non-empty elements "[2x2 double] elements") in each column of the matrix??
Hi all, I have the following matrix;
P =
Columns 1 through 2
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x2 double] [2x2 double]
[2x2 double] [2x2 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
Columns 3 through 4
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
[2x0 double] [2x2 double]
[2x2 double] [2x2 double]
[2x2 double] [2x2 double]
[2x0 double] [2x0 double]
[2x0 double] [2x0 double]
I want to get the number of the non-empty elements in each column and then their values.
For example; in Column 1, there are two non-empty elements [2x2 double]. To get their values; I should write like that; P{row_no,1} ;
>> P{7,1}
ans =
0.5878 0.71222
0 0
>> P{8,1}
ans =
0.78307 1.117
0 0
So, My question is; how to get the number of the non-empty elements in each column and also how to get the values of these non-empty elements?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2018년 8월 19일
nonempty_col1_mask = ~cellfun(@isempty, P(:,1));
nonempty_col2_mask = ~cellfun(@isempty, P(:,2));
num_nonempty_col1 = nnz(nonempty_col1_mask);
num_nonempty_col2 = nnz(nonempty_col2_mask);
nonempty_col1 = P(nonempty_col1_mask, 1);
nonempty_col2 = P(nonempty_col2_mask, 2);
  댓글 수: 3
Walter Roberson
Walter Roberson 2018년 8월 20일
I do not see any problem? Each column has entries which are 2D arrays, and the code correctly pulls out the 2D arrays into individual locations.
If you want all of the the 2D arrays merged together, giving a single 4 x 2 array for each of columns 1, 2, and 3, and a 6 x 2 array for column 4, then cell2mat(P(nonempty_col{j}, j)) .

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by