cell-array

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

独自空忆成欢 提交于 2019-12-04 19:26:47
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? 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) toc a = {[1 2 3]; [4 5]; [6 7 8 9]}; tic for ii=1:numel(a) a{ii} = [10 a{ii}]; end toc The results: Elapsed

Subsref with cells

折月煮酒 提交于 2019-12-04 10:37:55
This issue appeared when I was answering this question . It should be some stupid error I am doing, but I can't get what error it is… myMatrix = [22 33; 44 55] Returns: >> subsref(myMatrix, struct('type','()','subs',{{[1 2]}} ) ); ans = 22 44 While using it with cells: myCell = {2 3; 4 5} Returns: >> subsref(myCell,struct('type','{}','subs',{{[1 2]}} ) ); ans = 2 % WHATTT?? Shouldn't this be 2 and 4 Matlab?? Checking the subsref documentation , we see: See how MATLAB calls subsref for the expression: A{1:2} The syntax A{1:2} calls B = subsref(A,S) where S.type='{}' and S.subs={[1 2]}. This

How to find the vectors of the cell A that contain at least one element of the vector B?

不打扰是莪最后的温柔 提交于 2019-12-04 04:09:02
问题 How to find the vectors of A that contain at least one element of the vector B ? example: A = {[2 5],[8 9 2],[33 77 4],[102 6],[10 66 17 7 8 11],[110 99],[1 4 3],[15 41 88]} B = [5 77 41 66 7] Result = {[2 5],[33 77 4],[10 66 17 7 8 11],[15 41 88]} 回答1: Approaches With arrayfun and ismember - Result = A(arrayfun(@(n) any(ismember(B,A{n})),1:numel(A))) Or with arrayfun and bsxfun - Result = A(arrayfun(@(n) any(any(bsxfun(@eq,B(:),A{n}),2)),1:numel(A))) Or with arrayfun and setdiff - Result = A

Convert cell to double

痞子三分冷 提交于 2019-12-04 03:58:37
>> 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 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}]; C = [{1} {2} ; {'@CF'} {2}] C = [ 1] [2] '@CF' [2] D = cellfun(@isnumeric,C); C(~D)={nan} C = [ 1] [2] [NaN] [2] cell2mat(C) ans = 1 2

How to remove zero entries inside a cell array in MATLAB?

不问归期 提交于 2019-12-04 03:13:09
问题 I have a cell array in MATLAB, lets say cell_arr and it has zero entries as well as non-zeros cell entries. For example: cell_arr = {0, 0, 0, 0, 0, {1x3 cell}, {1x3 cell}, {1x3 cell}, {1x3 cell}}; Can somebody please tell how to remove these zero entries from the cell_arr or, to find the indices of non-zero entries? Additionally, I want to avoid for loop for performing this job. I already tried find function, however, find function is not applicable for cell arrays. I am wondering if there

MATLAB: comparison of cell arrays of string

别说谁变了你拦得住时间么 提交于 2019-12-03 19:31:13
问题 I have two cell arrays of strings, and I want to check if they contain the same strings (they do not have to be in the same order, nor do we know if they are of the same lengths). For example: a = {'2' '4' '1' '3'}; b = {'1' '2' '4' '3'}; or a = {'2' '4' '1' '3' '5'}; b = {'1' '2' '4' '3'}; First I thought of strcmp but it would require looping over one cell contents and compare against the other. I also considered ismember by using something like: ismember(a,b) & ismember(b,a) but then we

Why is a trailing comma in a cell array valid Matlab syntax?

泪湿孤枕 提交于 2019-12-03 11:19:23
问题 I was surprised today to discover that A = {1,2,3} and B = {1,2,3,} are both valid syntax in MATLAB. I would have expected the second statement to yield an error. As best as I can tell, they produce identical cell arrays ( all([A{:}]==[B{:}]) returns true). Is there a reason the second syntax is allowed? Is this a bug in the parser? Are A and B truly the same? Intriguingly, the following is not allowed: C = {1,2,3,,,} 回答1: These are more guesses, rather than an answer. One could check the

How to apply cellfun (or arrayfun or structfun) with constant extra input arguments?

天大地大妈咪最大 提交于 2019-12-03 09:36:49
问题 I want to apply a function to each element of a cell array -- so I have cellfun for that. However, the function takes two extra arguments (a string and a vector), which I want to keep constant for all the elements of the cell array; i.e. I'd like to do something like: cellfun(@myfun, cellarray, const1, const2) meaning: for i = 1:numel(cellarray), myfun(cellarray{i}, const1, const2); end Is there some way to do that without creating intermediate cell arrays containing numel(cellarray) copies

Why is a trailing comma in a cell array valid Matlab syntax?

一个人想着一个人 提交于 2019-12-03 01:40:25
I was surprised today to discover that A = {1,2,3} and B = {1,2,3,} are both valid syntax in MATLAB. I would have expected the second statement to yield an error. As best as I can tell, they produce identical cell arrays ( all([A{:}]==[B{:}]) returns true). Is there a reason the second syntax is allowed? Is this a bug in the parser? Are A and B truly the same? Intriguingly, the following is not allowed: C = {1,2,3,,,} thewaywewalk These are more guesses, rather than an answer. One could check the Symbol reference and find that the comma , can be used as Command or Statement Separator To enter

Reordering Cell Array by Array of Indices

牧云@^-^@ 提交于 2019-12-02 22:47:07
问题 Suppose I have a cell array x and an integer array y : x = {'apple', 'orange', 'banana', 'pear'}; y = [2 4 3 1]; In fact, y represents indices of x . I want to now create a cell array z with the elements of x reordered as specified by the order of these indices. This would give me: z = {'orange', 'pear', 'banana', 'apple'}; Can I do this in one line without having to loop through each element and place it in z in turn? 回答1: z = x(y); Because StackOverflow requires answers at least 30 chars