简体   繁体   中英

MATLAB : how to split and transpose a cell array?

I have the next cell composition (30*1 size) is important to note that is a cell array made of numbers and NOT of strings.

What I want to do is to split it first, according to the "," so each different number has to occupy a diffierent column. Like this:

细胞分裂

After this, I want to transpose all the numbers, so I want to have a final matrix size (7*30)

细胞转置

Can anyone help me to solve this?

Thanks in advance!

This is my version of the solution, type this in the Command Window in Matlab:

>> a={12,13,14,45;4,8,nan,nan;450,2,14,nan};  %% cell array
>> b=cell2mat(a); %% convert cell array to ordinary array of the underlying data type
>> c=transpose(b) %% transpose matrix b (or use the following syntax b.')

Some useful links :

  1. cell2mat
  2. transpose

Looking at the two images that you put in your question (though they look like being in Excel to me), I suppose that you have a cell-array with 30 lists (double-array; array with entries being of type number/double). Here I put a shorter example with 2 instead of 30.

dataInput={[1,2,3,4,5,6,7];[0,-1,0.5,2,5,6,7]}

It is a 2×1 cell array which each of its entries are a 1x7 array of doubles . Now to answer the first part of your question, ie separating entries of the lists to have each number as a single cell, not the whole list of 7 numbers as one cell, I use num2cell . And for the second part of your question, transposing the cells, I just add ' (single quotation). And to mix all of these cells into single cell-array, I use brackets. So here is one way to have a 7×2 cell array as you want.

dataOutput=[num2cell(dataInput{1})',num2cell(dataInput{2})']

You can also use the ' operator at the end (outside), like the following.

dataOutput2=[num2cell(dataInput{1});num2cell(dataInput{2})]'
% or
dataOutput3=vertcat(num2cell(dataInput{1}),num2cell(dataInput{2}))'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM