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

37 Ansichten (letzte 30 Tage)
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 Kommentar
Image Analyst
Image Analyst am 1 Feb. 2020
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Aug. 2018
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 Kommentare
Walter Roberson
Walter Roberson am 20 Aug. 2018
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)) .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by