필터 지우기
필터 지우기

Converting cell in double

조회 수: 7 (최근 30일)
Max1234
Max1234 2023년 1월 2일
편집: Stephen23 2023년 1월 3일
Hey Guys,
i have a 8942x1 cell and want to convert it into a 8942x1 double ? The final product should be look likes this:
[] 0
1 1
[] => 0
[] 0
[] 0
... ...
Thank you very much for your help!
Attached my file:

채택된 답변

Star Strider
Star Strider 2023년 1월 2일
편집: Star Strider 2023년 1월 2일
Try something like this —
LD = load(websave('ids','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1249257/ids.mat'));
ids = LD.ids
ids = 8942×1 cell array
{0×0 double} {[ 1]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
idsd = cell2mat(ids)
idsd = 966×1
1 1 1 1 1 1 1 1 1 1
idsmt = cellfun(@isempty,ids, 'Unif',0); % Detect Empty Cells
ids(cell2mat(idsmt)) = {0}; % Fill Missing Values With '0'
idsd = cell2mat(ids)
idsd = 8942×1
0 1 0 0 0 0 0 0 0 0
EDIT — Corrected typographical errors.
.
  댓글 수: 2
Max1234
Max1234 2023년 1월 2일
Perfect!! Thank you!
Star Strider
Star Strider 2023년 1월 2일
As always, my pleasure!

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

추가 답변 (1개)

Stephen23
Stephen23 2023년 1월 2일
편집: Stephen23 2023년 1월 3일
Simpler and more efficient:
S = load('ids.mat');
ids = S.ids
ids = 8942×1 cell array
{0×0 double} {[ 1]} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
Method one: robust indexing:
X = cellfun(@isscalar,ids);
V = zeros(size(ids));
V(X) = [ids{X}]
V = 8942×1
0 1 0 0 0 0 0 0 0 0
Method two: VERTCAT():
ids(~cellfun(@isscalar,ids)) = {0};
V = vertcat(ids{:})
V = 8942×1
0 1 0 0 0 0 0 0 0 0

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by