필터 지우기
필터 지우기

converting a cell array of doubles in a matrix

조회 수: 123 (최근 30일)
Haneya Qureshi
Haneya Qureshi 2018년 6월 19일
댓글: OCDER 2018년 6월 19일
I have a variable A which looks like this:
1×2 cell array
{1×9 double} {1×9 double}
I want to convert it to a matrix, i.e, the first row of matrix should be what's inside the A{1} and second row of matrix should be what's inside A{2}. A is a cell, but A{1} and A{2} are doubles. How can i do this?

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 6월 19일
A{1} = rand(1,9);
A{2} = rand(1,9);
cell2mat(A')
A =
1×2 cell array
{1×9 double} {1×9 double}
ans =
Columns 1 through 7
0.0844 0.3998 0.2599 0.8001 0.4314 0.9106 0.1818
0.1361 0.8693 0.5797 0.5499 0.1450 0.8530 0.6221
Columns 8 through 9
0.2638 0.1455
0.3510 0.5132
  댓글 수: 2
Haneya Qureshi
Haneya Qureshi 2018년 6월 19일
Thank you so much! This works perfectly!
Ameer Hamza
Ameer Hamza 2018년 6월 19일
You are welcome.

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

추가 답변 (1개)

OCDER
OCDER 2018년 6월 19일
A = {rand(1,10000), rand(1,10000)};
B = vertcat(A{:});
  댓글 수: 1
OCDER
OCDER 2018년 6월 19일
cell2mat is certainly the right function for converting cell arrays to matrices while preserving the MxN dimension of the cell array. However, vertcat is a built-in function (meaning super optimized) and will run ~2x faster for your case.
A = {rand(1,10000), rand(1,10000)};
t1 = timeit(@() vertcat(A{:}), 1);
t2 = timeit(@() cell2mat(A), 1);
fprintf('cell2mat(A'') time is %0.2f times slower than vertcat(A{:})\n', t2/t1);

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by