cell-array

Add the resulting array of each loop to a cell

自作多情 提交于 2021-02-17 05:52:09
问题 I have some algorithm including a while loop: while (condition) % do something and return a result array B end Let's say: -Loop 1: B1=[1 2 3 4 5....9]; -Loop 2: B2=[10 11 12....15]; -Loop 3: B3=[16 17 18 19] ; -Loop 4: B4=[20 21 22....30]; How can I create a cell A={B1,B2,B3,B4} when the loop is finished? For my real data, the while loop may be looping 100 times or more, the above is a simplification. 回答1: You can make use of the end keyword % Initialise empty cell array A = {}; while

Combining Anonymous Functions in MATLAB

有些话、适合烂在心里 提交于 2021-02-08 04:50:18
问题 I have a cell array of anonymous function handles, and would like to create one anonymous function that returns the vector containing the output of each function. What I have: ca = {@(X) f(X), @(X)g(X), ...} What I want: h = @(X) [ca{1}(X), ca{2}(X), ...] 回答1: Yet another way to it: You can use cellfun to apply a function to each cell array element, which gives you a vector with the respective results. The trick is to apply a function that plugs some value into the function handle that is

Combining Anonymous Functions in MATLAB

孤者浪人 提交于 2021-02-08 04:49:36
问题 I have a cell array of anonymous function handles, and would like to create one anonymous function that returns the vector containing the output of each function. What I have: ca = {@(X) f(X), @(X)g(X), ...} What I want: h = @(X) [ca{1}(X), ca{2}(X), ...] 回答1: Yet another way to it: You can use cellfun to apply a function to each cell array element, which gives you a vector with the respective results. The trick is to apply a function that plugs some value into the function handle that is

Matlab: How to convert cell array to string array?

大城市里の小女人 提交于 2021-01-28 10:53:10
问题 I have a cell array sized 14676x117 call myCellArray . I want to extract values stored in myCellArray{2:14676,1} in an string array. runnning below script only returns a single string value and do not return an string array. >> y= myCellArray{2:14676,1} y = "test1" How can I convert this cell array range to and string array? 回答1: Try: y = string(myCellArray{2:14675, 1}) If you have MATLAB 2016b or newer, this should work. Source: Create String Arrays 回答2: Use char command: c = char

Saving dictionaries from Python to Matlab with Scipy

孤人 提交于 2021-01-27 13:06:02
问题 I'm finding some problems to save my neat generated data into .mat files. I thought it was more straightforward with Scipy, but it seems I'm getting something wrong. This is an example of data I want to save: out = {'features': array([[ 5.00088905e+01, 1.51847522e+01, 4.93513862e+01, 3.76548415e+00, -3.96946513e+01, -2.11885850e+01, 9.85304035e+00, -6.30005764e+00, 1.19987435e+01, 3.89762536e+00, -1.31554755e+00, -1.66890836e+01, 4.75289017e-02, 3.65829480e-01, -4.77872832e-01, 1.13641908e+00

cell array, add suffix to every string

半世苍凉 提交于 2021-01-27 07:32:04
问题 Suppose I have a cell array containing strings: c = {'foo1', 'foo2', 'foo3'} I now want to add the same suffix " bar " to each string, such that the cell array becomes: c = {'foo1bar', 'foo2bar', 'foo3bar'} Is there a shortcut to doing this, without explicitly looping through each element? 回答1: strcat operates on cell arrays: >> c = {'foo1', 'foo2', 'foo3'} c = 'foo1' 'foo2' 'foo3' >> c2 = strcat(c,'bar') c2 = 'foo1bar' 'foo2bar' 'foo3bar' 回答2: What about using cellfun: c=cellfun(@(x) strcat

cell array, add suffix to every string

旧时模样 提交于 2021-01-27 07:30:23
问题 Suppose I have a cell array containing strings: c = {'foo1', 'foo2', 'foo3'} I now want to add the same suffix " bar " to each string, such that the cell array becomes: c = {'foo1bar', 'foo2bar', 'foo3bar'} Is there a shortcut to doing this, without explicitly looping through each element? 回答1: strcat operates on cell arrays: >> c = {'foo1', 'foo2', 'foo3'} c = 'foo1' 'foo2' 'foo3' >> c2 = strcat(c,'bar') c2 = 'foo1bar' 'foo2bar' 'foo3bar' 回答2: What about using cellfun: c=cellfun(@(x) strcat

How to remove cell elements that are a subset of a larger element? (Matlab)

房东的猫 提交于 2020-05-15 06:20:26
问题 For example, if I have a cell with arrays listed inside: C = {[1,2,3,4], [3,4], [2], [4,5,6], [4,5], [7]} I want to output: D = {[1,2,3,4], [4,5,6], [7]} What is the most efficient way to remove cell elements that are already included/subset in another larger element? My existing algorithm loops through each element, compares it to each element in the new cell list and updates the new cell list accordingly but it is extremely slow and inefficient (my original cell contains >200 array elements

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

女生的网名这么多〃 提交于 2020-01-21 11:55:28
问题 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

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

心不动则不痛 提交于 2020-01-21 04:33:29
问题 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