cell-array

Convert cell to double

一个人想着一个人 提交于 2020-01-01 08:59:06
问题 >> C = [{1} {2} ; {'@CF'} {2}] C = [ 1] [2] '@CF' [2] >> whos C Name Size Bytes Class Attributes C 2x2 478 cell How can I convert C into double so that: >> C C = 1 2 NaN 2 I've tried str2double(C) . It returns: NaN NaN NaN NaN 回答1: Find the non numeric values with isnumeric, queried by cellfun. Use that with logical indexing to extract the numeric values: C = [{1} {2} ; {'@CF'} {2}]; isnum = cellfun(@isnumeric,C); result = NaN(size(C)); result(isnum) = [C{isnum}]; 回答2: C = [{1} {2} ; {'@CF'}

Concatenate two cell arrays of strings with a space in between?

北战南征 提交于 2019-12-30 11:00:18
问题 How can I concatenate: A = {'hello'; 'hi'; 'hey'} with B = {'Ben'; 'Karen'; 'Lisa'} with a space in between to get: C = {'hello Ben'; 'hi Karen'; 'hey Lisa'} Is there a fast non-looping way? 回答1: You can use strcat() , although it performs a loop: strcat(A,{' '}, B) where the blank is preserved by enclosing it within a cell. Alternatively, FEX:CStrCatStr is a mex routine which achieves a 10x speedup (depending on testing environment): CStrCatStr(A,' ', B) 回答2: A faster (albeit less elegant)

MATLAB: how to sort a matrix by a specific column name and also let the row names follow the order?

只愿长相守 提交于 2019-12-25 06:45:50
问题 I am new to MATLAB. I have a data structure named da . I want to sort the first column of da.mat and want to let da.rid and the other columns to follow the rearranged order. da.cid contains the column names and da.rid contains the row IDs. da = mat: [22268x377 single] rid: {22268x1 cell} rhd: {''} rdesc: {22268x1 cell} cid: {377x1 cell} chd: {0x1 cell} cdesc: {377x0 cell} Also, if I want to use some other column instead of the first column of da.mat and which I will get from da.cid , how can

Mix letters of a string in alphabetical order in matlab

六月ゝ 毕业季﹏ 提交于 2019-12-25 02:18:09
问题 I have the cell array of strings in matlab. I want to sort letters in every string in alphabetical order. How can I do that? For example, if I have ['dcb','aetk','acb'}] , I want it to be: ['bcd','aekt','abc'] . 回答1: The handy helper here is cellfun, with the correct option for nonscalar output - we tell it to run sort on each element of the cell array in turn: >> a = {'dcb' 'aetk' 'acb'} a = { [1,1] = dcb [1,2] = aetk [1,3] = acb } >> b = cellfun(@sort, a, 'UniformOutput', false); b = { [1,1

How to display formatted numbers from a cell array with n decimal places

一个人想着一个人 提交于 2019-12-25 01:51:21
问题 I have a cell array that contains both numbers and strings. I want to print the cell array so that I will see only 5 numbers after the decimal point. For example: c{1, 1} = pi; c{2, 1} = pi / 2; c{3, 1} = pi / 4; c{4, 1} = 2 ^ 0.5; After using format long this is what I receive: >> c c = [3.141592653589793] [1.570796326794897] [0.785398163397448] [1.414213562373095] But I want that the output will be: >> c c = [3.14159] [1.57079] [0.78539] [1.41421] 回答1: A possible solution is to generate a

How to remove all cells which contain supersets of other cells?

荒凉一梦 提交于 2019-12-24 22:25:22
问题 I am working in text mining. I have 23 sentences that I have extracted from a text file along with 6 frequent words extracted from the same text file. For frequent words, I created 1D array which shows words and in which sentences they occur. After that I took the intersection to show which word occurs with which each of other remaining words in sentence: OccursTogether = cell(length(Out1)); for ii=1:length(Out1) for jj=ii+1:length(Out1) OccursTogether{ii,jj} = intersect(Out1{ii},Out1{jj});

What is the difference between the data stored in a cell and the data stored as double in MATLAB?

眉间皱痕 提交于 2019-12-24 17:04:46
问题 I have two variables who look exactly the same to me, but one is <double> and the other is <cell> . In the code it seems that they are converted by cell2mat . I understand it is a question of data storage but I just don't see the difference and the definition of cell and double for this. 回答1: Adding to nrz's answer, it is noteworthy that there is an additional memory overhead when storing cell arrays. For instance, consider the following code: A = 1:5 B = {A} C = num2cell(A) whos which

Character count of regular expression in cells in MATLAB

≯℡__Kan透↙ 提交于 2019-12-24 16:53:27
问题 Earlier I got some help as to how to make a script that will extract hashtags from a list of tweets and put them into an array of cells. I used this as my code, inside a for loop hashtagCell{i} = regexp(textRead{i}, '#[A-z]*', 'match'); This works for what it is supposed to do, but now I'm trying to find the average character length of the hashtags, so I need to be able to add the character length of each hashtag pulled out by the above function and add them together. However, when I try to

Comparing strings in cell arrays

安稳与你 提交于 2019-12-24 04:02:16
问题 I'm trying to find the most frequent word in a list of words. Here is my code so far: uniWords = unique(lower(words)); for i = 1:length(words) for j = 1:length(uniWords) if (uniWords(j) == lower(words(i))) freq(j) = freq(j) + 1; end end end When I try to run the script, I get the following error: Undefined function 'eq' for input arguments of type 'cell'. Error in Biweekly3 (line 106) if (uniWords(j) == lower(words(i))) Any help is appreciated! 回答1: You need to extract the contents of the

Left join for cell arrays in MATLAB

纵饮孤独 提交于 2019-12-23 13:09:03
问题 I've 2 cell arrays in MATLAB, for example: A= {jim,4,paul,5 ,sean ,5,rose, 1} and the second: B= {jim, paul, george, bill, sean ,rose} I want to make an SQL left join so I'll have all the values from B and their match from A. If they don't appear in A so it will be '0'. means: C= {jim, 4, paul, 5, george, 0, bill, 0, sean, 5, rose, 1} didn't find any relevant function for help. thanks. 回答1: Approach #1 %// Inputs A= {'paul',5 ,'sean' ,5,'rose', 1,'jim',4} B= {'jim', 'paul', 'george', 'bill',