필터 지우기
필터 지우기

convert a cell (Rx1 cell) to a vector (Rx1 double)

조회 수: 1 (최근 30일)
Alberto Acri
Alberto Acri 2023년 12월 7일
편집: Stephen23 2023년 12월 8일
Hi. I want to convert the cell 'rows_check_n' into a vector of type double with the numbers inside the cell.
I tried 'cell2mat' but it is not clear to me why it transforms 270x1 cell into a vector 257x1 double. How can I solve this?
rows_check_n = load("rows_check_n.mat");
rows_check_n = rows_check_n.rows_check_n; % 270x1 cell
rows_check_n_double = cell2mat(rows_check_n); % 257x1 double

답변 (2개)

Walter Roberson
Walter Roberson 2023년 12월 7일
이동: Walter Roberson 2023년 12월 7일
13 entries in the cell are empty.
rows_check_n = load("rows_check_n.mat");
rows_check_n = rows_check_n.rows_check_n; % 270x1 cell
disp(find(cellfun(@isempty, rows_check_n)))
99 100 101 102 103 104 105 106 107 108 109 110 111

Stephen23
Stephen23 2023년 12월 8일
편집: Stephen23 2023년 12월 8일
"but it is not clear to me why it transforms 270x1 cell into a vector 257x1 double"
It is easy to check your data (you have been using MATLAB for over three years now, you can do this too):
S = load("rows_check_n.mat");
C = S.rows_check_n % 270x1 cell
C = 270×1 cell array
{[106798]} {[106799]} {[106800]} {[106801]} {[106802]} {[106803]} {[107309]} {[107310]} {[107311]} {[107312]} {[107313]} {[107314]} {[107315]} {[107820]} {[107821]} {[107822]}
X = cellfun(@isscalar,C);
all(X) % are all of them scalar as you believe? (hint: no)
ans = logical
0
Lets replace the non-scalar elements of C with NaNs, then you get a vector of the same size as C:
V = nan(size(C));
V(X) = [C{X}]
V = 270×1
106798 106799 106800 106801 106802 106803 107309 107310 107311 107312

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by