cell-array

Concatenate subcells through one dimension of a cell array without using loops in MATLAB

笑着哭i 提交于 2019-12-07 08:27:26
问题 I have a cell array. Each cell contains a vector of variable length. For example: example_cell_array=cellfun(@(x)x.*rand([length(x),1]),cellfun(@(x)ones(x,1), num2cell(ceil(10.*rand([7,4]))), 'UniformOutput', false), 'UniformOutput', false) I need to concatenate the contents of the cells down through one dimension then perform an operation on each concatenated vector generating scalar for each column in my cell array (like sum() for example - the actual operation is complex, time consuming,

How to sort a structure array

情到浓时终转凉″ 提交于 2019-12-07 06:02:47
问题 How do I sort the oo structure array alphabetical order by item name. oo = struct('Item', {'Quill','Ink Pen', 'Pencil'}, 'Cost', {10, 2, 1}) I tried using the sort() function but it didn't work? Thank you. 回答1: First index your field, in this case oo.Items which returns a comma separated list. For string data use {} to concatenate to a cell of strings, otherwise use [] to get an array: %get the right order using second output of sort [~,index]=sort({oo.Item}) %sort it oo=oo(index) 来源: https:/

Strcmp for cell arrays of unequal length in MATLAB

余生长醉 提交于 2019-12-07 02:02:46
问题 Is there an easy way to find a smaller cell array of strings within a larger one? I've got two lists, one with unique elements, and one with repeating elements. I want to find whole occurrences of the specific pattern of the smaller array within the larger. I'm aware that strcmp will compare two cell arrays, but only if they're equal in length. My first thought was to step through subsets of the larger array using a loop, but there's got to be a better solution. For example, in the following:

How to insert a number to every cell of a cell array in Matlab?

ぐ巨炮叔叔 提交于 2019-12-06 14:16:04
问题 I have a cell array like this: a = {[1 2 3]; [4 5]; [6 7 8 9]}; and want to insert, for example, 10 to the beginning of every cell to have this: >> a{:} ans = 10 1 2 3 ans = 10 4 5 ans = 10 6 7 8 9 Is it possible to do it without any for loop? 回答1: You can use CELLFUN with anonymous function: b = cellfun(@(x)[10 x],a,'UniformOutput',0) To answer @tmpearce comment I used a simple script to measure running time: a = {[1 2 3]; [4 5]; [6 7 8 9]}; tic a = cellfun(@(x)[10 x],a,'UniformOutput',0)

Delete a cell array column

倾然丶 夕夏残阳落幕 提交于 2019-12-06 07:18:13
问题 Placed simple values into the cell array for testing. model{1,1}=1;model{1,2}=2;model{1,3}=3; model{2,1}=4;model{2,2}=5;model{2,3}=6; i=2;//I want to remove the second column temp={ model{:,1:i-1} model{:,i+1:size(model,2)} } I wanted a result like this: temp = [1] [3] [4] [6] But I'm getting this: temp = [1] [4] [3] [6] How can I get this right? p.s: for anyone working on Cell Arrays, there's a nice technique for appending here. 回答1: You can reshape or delete the cells themselves using ()

Access predefined elements of cells

笑着哭i 提交于 2019-12-06 07:13:17
I have a cell array A [1x80] in which each element is a cell array itself [9x2]. I have also a vector B representing a group of selected cells of A and I want to extract the element {2,2} of each selected cell. I tried with a simple A(1,B){2,2} but of course it doesn't work.... Can you help me? How about this: A = {{1 2; 3 4}, {5 6;7 8}, {9 0; 1 2}; {3 4; 5 6}, {7 8; 9 0}, {11 22; 33 44}}; B = [2,3] [cellfun(@(x)x(2,2), A){1, B}] ans = 8 2 EDIT: The above actually only works in octave. As @Amro points out, to modify it to work in Matlab you need to use a temporary variable: temp = cellfun(@(x

Concatenate subcells through one dimension of a cell array without using loops in MATLAB

女生的网名这么多〃 提交于 2019-12-05 12:08:29
I have a cell array. Each cell contains a vector of variable length. For example: example_cell_array=cellfun(@(x)x.*rand([length(x),1]),cellfun(@(x)ones(x,1), num2cell(ceil(10.*rand([7,4]))), 'UniformOutput', false), 'UniformOutput', false) I need to concatenate the contents of the cells down through one dimension then perform an operation on each concatenated vector generating scalar for each column in my cell array (like sum() for example - the actual operation is complex, time consuming, and not naturally vectorisable - especially for diffent length vecotrs). I can do this with loops easily

Fill cell array with elements based on the indices MATLAB

混江龙づ霸主 提交于 2019-12-05 07:02:11
问题 I have a n*m cell array Cell_In: a b * * * * c * * d * * * f * --> represents empty string (''). Here is what I need: a b a b a b c b c d c d c f For a particular column I need to fill the empty cell with the previous non-empty cell until another non-empty cell is found. Following is the code what I wrote. b = ~cellfun(@isempty,a); c = [find(b(:,1) == 1);size(a,1)+1]; e = diff(c); d = [find(b(:,2) == 1);size(a,1)+1]; f = diff(d); s1 = ''; s2 = ''; for i = 1:length(e) s1 = [s1,repmat(a(c(i),1)

How can I divide a matrix into unequally-sized submatrices?

回眸只為那壹抹淺笑 提交于 2019-12-05 06:55:12
I am wondering if it is possible to use the mat2cell function to divide an MxN matrix into 10 submatrices with the same column size, N , and approximately the same row size ~M/10 ? If mod(M, 10) == 0 then all submatrices will have the same size, otherwise a few matrices will have +/-1 row. Is this possible via the mat2cell function? For reference, if the row sizes are all the same it's fairly straightforward, as explained here: How to divide a matrix into equals parts? Here's a simple solution using the functions linspace , round , and diff : [M, N] = size(mat); % Matrix size nSub = 10; %

Strcmp for cell arrays of unequal length in MATLAB

ぐ巨炮叔叔 提交于 2019-12-05 06:47:00
Is there an easy way to find a smaller cell array of strings within a larger one? I've got two lists, one with unique elements, and one with repeating elements. I want to find whole occurrences of the specific pattern of the smaller array within the larger. I'm aware that strcmp will compare two cell arrays, but only if they're equal in length. My first thought was to step through subsets of the larger array using a loop, but there's got to be a better solution. For example, in the following: smallcellarray={'string1',... 'string2',... 'string3'}; largecellarray={'string1',... 'string2',...