cell-array

Cell elements as comma separated input arguments for varargin function

余生长醉 提交于 2019-12-01 20:41:09
问题 Imagine a function with a variable number of input arguments, alternately asking for a string and a value. myfunction('string1',value1,'string2',value2,...) e.g. myfunction('A',5,'B',10) I want to keep the ability to call the function like that and I dont want to change the evaluation of varargin inside the function. (Except ('string1','string2',...,value1,value2,...) if that helps) But I also have my input strings and values stored in a cell array inputvar <4x1 cell> : inputvar = 'A' [5] 'B'

How to find index of the last non-empty element in a cell array

為{幸葍}努か 提交于 2019-12-01 19:50:35
I initialized a very long cell array(vector?) train_labels = cell(16218, 1); These will be populated using files from 50 different folders, to make sure that the files are indexed in the right location, I need the index of the last cell array that was written to. For example after reading one folder, the index in train_labels had reached 5406 . Now to read the images from the next folder they must be saved to the next index that is 5407 . To make that work I need to find the location of the last non-empty array in train_labels . Since the simple find(train_labels,1,'last') does not work on

How can I sort the elements of a cell?

99封情书 提交于 2019-12-01 17:56:16
I have a cell like this: A{1,1}=[ 1 ;2; 3;]; A{2,1}=[ 4 ;2;]; A{3,1}=[ 3 ;2; 5; 4; 6;]; ... A{N,1}=[ 10 ;2;5; 7;]; %N is very large. In other words, the number of columns in each element of this cell is different, with no definite pattern. Now, I want to sort these elements based on the element at the first column . I mean, I want the result to be like this: Asorted{1,1}=[ 1 ;2; 3;]; Asorted{2,1}=[ 3 ;2; 5; 4; 6;]; Asorted{3,1}=[ 4 ;2;]; ... Asorted{N,1}=[ 10 ;2;5; 7;]; Currently I use this function: function Asorted = sortcell(A) B=[]; nrows = size(A,1); for i=1:nrows % this for-loop is slow

How can I sort the elements of a cell?

丶灬走出姿态 提交于 2019-12-01 17:32:56
问题 I have a cell like this: A{1,1}=[ 1 ;2; 3;]; A{2,1}=[ 4 ;2;]; A{3,1}=[ 3 ;2; 5; 4; 6;]; ... A{N,1}=[ 10 ;2;5; 7;]; %N is very large. In other words, the number of columns in each element of this cell is different, with no definite pattern. Now, I want to sort these elements based on the element at the first column . I mean, I want the result to be like this: Asorted{1,1}=[ 1 ;2; 3;]; Asorted{2,1}=[ 3 ;2; 5; 4; 6;]; Asorted{3,1}=[ 4 ;2;]; ... Asorted{N,1}=[ 10 ;2;5; 7;]; Currently I use this

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

╄→гoц情女王★ 提交于 2019-12-01 09:53:31
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? 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) A faster (albeit less elegant) alternative to strcat that concatenates strings is a combination of the sprintf and textscan commands: C = [A; B];

Matlab - sort cell array of objects by property

我的未来我决定 提交于 2019-12-01 05:05:51
问题 Suppose I had a class named Foo, with a datenum property named DateTime. If I had a cell array collection of Foo objects, how would I sort that according to each object's DateTime property? I have seen references to overloading the sort method and working with arrays of objects, however I'm using a cell array due to dynamic sizing and those instructions aren't holding up. Anybody got some suggestions? Cheers 回答1: The simplest approach is to extract the time-values into a vector, sort that,

MATLAB/OCTAVE Extracting elements from different sized vectors in a cell array

て烟熏妆下的殇ゞ 提交于 2019-12-01 04:59:25
问题 I have a simple question but I can't figure it out or find it anywhere. I have a cell array where c{1} is a vector and c{2} is a vector but of different lengths, up to c{i}. What I want is one vector that is [c{1};c{2};c{3}...c{i}]. What is the most efficient way to do this? 回答1: The following one-liner even works for completely inconsistent inputs: result = [cell2mat(cellfun(@(x) x(:), A, 'uni', 0)')]' Example: for: A{1} = [1, 2, 3, 4, 5]; A{2} = [6; 7; 8; 9]; A{3} = [10, 12; 11, 13]; it

“Flattening” a cell array

风流意气都作罢 提交于 2019-12-01 02:40:20
I have created a function which takes vectors for input variables and returns a cell array for each set of inputs. The final output variable (out) seems to consist of a 2x1 cell containing two 1x5 cells. I have provided a screenshot of this below: I am just trying to figure out how to flatten the cell array (out) to be a 2x5 cell array. One way to achieve that would be - vertcat(cell_array1{:}) If your cell has unequal number of elements in each row , maybe this might work better vector=[cell_array{:}] 来源: https://stackoverflow.com/questions/23334819/flattening-a-cell-array

Outputing cell array to CSV file ( MATLAB )

爷,独闯天下 提交于 2019-11-30 23:29:56
I've created a m x n cell array using cell(m,n) , and filled each of the cells with arbitrary strings. How do I output the cell array as a CSV file, where each cell in the array is a cell in the CSV 'spreadsheet'. I've tried using cell2CSV , but I get errors ... Error in ==> cell2csv at 71 fprintf(datei, '%s', var); Caused by: Error using ==> dlmwrite at 114 The input cell array cannot be converted to a matrix. Any guidance will be well received :) Here is a somewhat vectorized solution: %# lets create a cellarray filled with random strings C = cell(10,5); chars = char(97:122); for i=1:numel(C

Find unique rows of a cell array considering all possible permutations on each row

回眸只為那壹抹淺笑 提交于 2019-11-30 19:30:44
I have cell array A of dimension m * k . I want to keep the rows of A unique up to an order of the k cells . The "tricky" part is "up to an order of the k cells" : consider the k cells in the i th row of A , A(i,:) ; there could be a row j of A , A(j,:) , that is equivalent to A(i,:) up to a re-ordering of its k cells, meaning that for example if k=4 it could be that: A{i,1}=A{j,2} A{i,2}=A{j,3} A{i,3}=A{j,1} A{i,4}=A{j,4} What I am doing at the moment is: G=[0 -1 1; 0 -1 2; 0 -1 3; 0 -1 4; 0 -1 5; 1 -1 6; 1 0 6; 1 1 6; 2 -1 6; 2 0 6; 2 1 6; 3 -1 6; 3 0 6; 3 1 6]; h=7; M=reshape(G(nchoosek(1