Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert doubles in a cell array into a vector in MATLAB

How can I take the first row of a cell array that contains doubles and insert it into a vector, without using a 'for' loop?

like image 551
ZalNas Avatar asked Dec 29 '22 00:12

ZalNas


2 Answers

You can use curly braces to get entries from the cell array as a comma-separated list, then collect those values into a row vector using square brackets. Here's an example:

>> C = num2cell(magic(5))    %# A sample cell array

C = 

    [17]    [24]    [ 1]    [ 8]    [15]
    [23]    [ 5]    [ 7]    [14]    [16]
    [ 4]    [ 6]    [13]    [20]    [22]
    [10]    [12]    [19]    [21]    [ 3]
    [11]    [18]    [25]    [ 2]    [ 9]

>> vec = [C{1,:}]      %# Put the first row in a vector

vec =

    17    24     1     8    15
like image 77
gnovice Avatar answered Dec 31 '22 12:12

gnovice


Like this?

avector = cell2mat(acellarray(1,:));
like image 40
yuk Avatar answered Dec 31 '22 14:12

yuk