cell-array

Create matrices from a given cell-array of strings with different lengths

一个人想着一个人 提交于 2019-11-29 12:54:25
I have 3 sequences in a cell-array: Input_cell= {'ABCD','ACD', 'ABD'} S1= 'ABCD' % which means A<B<C<D S2= 'ACD' % which means A<C<D % missing B in the full string of 'ABCD' S3= 'ABD' % which means A<B<D % missing C in the full string of 'ABCD' I want to convert each of the strings in the Input_cell into a matrix M ( i -by- j ) which has to satisfy these conditions: M(i,j) and M(j,i) are random M(i,i) = 0.5 M(i,j) + M(j,i) = 1 M(i,j) < M(j,i) For example if A<B then M(A,B) < M(B,A) For example if we have S1 = 'ABCD' (which means A<B<C<D ), the M1 matrix will be expected as follows: A B C D A 0

How to force MATLAB to return all values in a nested function call?

匆匆过客 提交于 2019-11-29 12:45:46
I find it impossible to write MATLAB code without creating a huge number of superfluous, single-use variables. For example, suppose function foo returns three column vectors of exactly the same size. Say: function [a, b, c] = foo(n) a = rand(n, 1); b = rand(n, 1); c = rand(n, 1); end Now, suppose that bar is a function that expect as imput a cell array of size (1, 3) . function result = bar(triplet) [x, y, z] = triplet{:}; result = x + y + z; end If I want to pass the results of foo(5) , I can do it by creating three otherwise-useless variables: [x, y, z] = foo(5); result = bar({x, y, z}); Is

Fastest way of finding repeated values in different cell arrays of different size

Deadly 提交于 2019-11-29 11:32:23
The problem is the following: I have a cell array of the form indx{jj} where each jj is an array of 1xNjj , meaning they all have different size. In my case max(jj)==3 , but lets consider a general case for the shake of it. How would you find the value(s) repeated in all the jj i the fastest way? I can guess how to do it with several for loops, but is there a "one (three?) liner"? Simple example: indx{1}=[ 1 3 5 7 9]; indx{2}=[ 2 3 4 1]; indx{3}=[ 1 2 5 3 3 5 4]; ans=[1 3]; Almost no-loop approach (almost because cellfun essentially uses loop(s) inside it, but it's effect here is minimal as we

strsplit: undefined function for input type 'char'

流过昼夜 提交于 2019-11-29 01:32:39
I have a <20x1> cell array and each of them stores some data in the form of a string (as it appears to me!!!). I want to access each element of the cell as an individual string and split is in words. The cell array I have is <20x1> cell array and to access each element as a cell I am using a for loop. for i=1:20 line=newline{i} end It shows me a all the elements within the array. Now since line is a string, I apply strsplit function to retrieve the words in the string. for i=1:20 words(i,:)=strsplit(line) end This gives me an error message : ??? Undefined function or method 'strsplit' for

How do I detect empty cells in a cell array?

心不动则不痛 提交于 2019-11-28 18:36:55
How do I detect empty cells in a cell array? I know the command to remove the empty cell is a(1) = [] , but I can't seem to get MATLAB to automatically detect which cells are empty. Background: I preallocated a cell array using a=cell(1,53) . Then I used if exist(filename(i)) and textscan to check for a file, and read it in. As a result, when the filename(i) does not exist, an empty cell results and we move onto the next file. When I'm finished reading in all the files, I would like to delete the empty cells of a . I tried if a(i)==[] Use CELLFUN %# find empty cells emptyCells = cellfun(

Generalization of mat2str to cell arrays

落爺英雄遲暮 提交于 2019-11-28 12:04:20
I sometimes miss a function to produce a string representation of a (possibly nested) cell array. It would be a generalization of mat2str , which only works for non-cell arrays (of numeric, char or logical type). Given an array x , how to obtain a string representation y , such that evaluating this string produces x ? For example, the input x = {[10 20], {'abc'; false; true;}}; should produce an output string like y = '{[10 20], {''abc''; false; true}}'; (or some variation regarding spacing an separators), such that isequal(x, eval(y)) is true . This process, transforming a data structure into

How do I find a specific cell within a cell array?

半城伤御伤魂 提交于 2019-11-28 10:31:19
问题 Let's say I have a cell array containing 1x2 cells. eg. deck = {{4,'c'},{6,'s'}...{13,'c'}...{6,'d'}} How can I find the index of a specific cell? E.g I want to find the index of the cell with the values {13,'c'} . Thanks! 回答1: Another method I can suggest is to operate on each column separately. We could use logical operators on each column to search for cards in your cell array that contain a specific number in the first column, followed by a specific suit in the second column. To denote a

How to average over a cell-array of arrays?

白昼怎懂夜的黑 提交于 2019-11-28 07:25:23
问题 I have a cell array c of equal-sized arrays, i.e. size(c{n}) = [ m l ... ] for any n . How can I get the mean values (averaging over the cell array index n ) for all array elements in one sweep? I thought about using cell2mat and mean but the former does not add another dimension but changes l to l*n . And looping manually of course takes like forever... 回答1: If all of your arrays are the same size, it makes more sense to store them in a matrix rather than a cell array. That makes it easier

How to force MATLAB to return all values in a nested function call?

假如想象 提交于 2019-11-28 06:11:58
问题 I find it impossible to write MATLAB code without creating a huge number of superfluous, single-use variables. For example, suppose function foo returns three column vectors of exactly the same size. Say: function [a, b, c] = foo(n) a = rand(n, 1); b = rand(n, 1); c = rand(n, 1); end Now, suppose that bar is a function that expect as imput a cell array of size (1, 3) . function result = bar(triplet) [x, y, z] = triplet{:}; result = x + y + z; end If I want to pass the results of foo(5) , I

How can I create/process variables in a loop in MATLAB?

会有一股神秘感。 提交于 2019-11-28 01:32:52
I need to calculate the mean, standard deviation, and other values for a number of variables and I was wondering how to use a loop to my advantage. I have 5 electrodes of data. So to calculate the mean of each I do this: mean_ch1 = mean(ch1); mean_ch2 = mean(ch2); mean_ch3 = mean(ch3); mean_ch4 = mean(ch4); mean_ch5 = mean(ch5); What I want is to be able to condense that code into a line or so. The code I tried does not work: for i = 1:5 mean_ch(i) = mean(ch(i)); end I know this code is wrong but it conveys the idea of what I'm trying to accomplish. I want to end up with 5 separate variables