accumarray

Letting accumarray output a table

柔情痞子 提交于 2020-02-03 05:09:04
问题 accumarray uses two rows of indices to create a matrix with elements on the location of valid index pairs with a value assigned by the specified function, e.g.: A = [11:20]; B = flipud([11:20]); C = 1:10; datamatrix = accumarray([A B],C); This way datamatrix will be a 20x20 matrix with values. If the values of A and B however are very large, this will result in a mostly empty matrix, with a small batch of data in the far corner. To circumvent this, one might set accumarray to issparse :

Calculating a 2D joint probability distribution

笑着哭i 提交于 2020-01-02 07:44:11
问题 I have many points inside a square. I want to partition the square in many small rectangles and check how many points fall in each rectangle, i.e. I want to compute the joint probability distribution of the points. I am reporting a couple of common sense approaches, using loops and not very efficient: % Data N = 1e5; % number of points xy = rand(N, 2); % coordinates of points xy(randi(2*N, 100, 1)) = 0; % add some points on one side xy(randi(2*N, 100, 1)) = 1; % add some points on the other

`accumarray` makes anomalous calls to its function argument

别说谁变了你拦得住时间么 提交于 2019-12-19 05:45:13
问题 Short version: The function passed as the fourth argument to accumarray sometimes gets called with arguments that are not consistent with specifications encoded the first argument to accumarray . As a result, functions used as arguments to accumarray must test for what are, in effect, anomalous conditions. The question is: how can an a 1-expression anonymous function test for such anomalous conditions? And more generally: how can write anonymous functions that are robust to accumarray 's

Is there an accumarray() that takes matrix as `val`?

我只是一个虾纸丫 提交于 2019-12-17 19:17:10
问题 accumarray()'s val argument must be a vector. In my case I need columns of a matrix to be summed (or averaged). Is there a function or a method to achieve this? What I am doing now is in a for loop I am summing column values separately: for iCol = 1:nCols means(:,iCol) = accumarray(labels', X(:,iCol)); end 回答1: One solution is to replicate the row indices in labels and add another column of column indices. Then you can reshape X into a column vector and apply accumarray once: labels = [repmat

Count occurences of strings in column - Matlab

拥有回忆 提交于 2019-12-14 02:26:37
问题 I have a column with the following data: Size: 100x7 val = USA USA France USA France I want to show the data on pie chart. to do this, I need to know how much USA occur in this column, and so on. I read about the functions unique,accumarray but I dont success I would like to get some suggestions how to do that. Thanks. 回答1: You can use unique with histc - %// Get countries and their occurences [countries,~,id] = unique(cellstr(val),'stable') occurrences = histc(id,1:max(id)) You can then

Joint Entropy of audio files

筅森魡賤 提交于 2019-12-13 01:06:55
问题 So, after trying a heavy cicle-based function for calculating the joint entropy of two sources of information, I found this useful MATLAB function, accumarray , and tried the following code: function e = jointEntropy(fonte1, fonte2) i = double(fonte1(:))+ 1; j = double(fonte2(:)) + 1; subs = [i j]; f = accumarray(subs, ones(length(fonte1), 1)); p = f / length(fonte1); freq = f ~= 0; prob = p(freq); e = -sum(prob.*log2(prob)); end , where fonte1 and fonte2 are the information sources, 1xN

Fast rolling correlation in Matlab

别来无恙 提交于 2019-12-08 10:44:46
问题 I am trying to derive a function for calculating a moving/rolling correlation for two vectors and speed is a high priority, since I need to apply this function in an array function. What I have (which is too slow) is this: Data1 = rand(3000,1); Data2 = rand(3000,1); function y = MovCorr(Data1,Data2) [N,~] = size(Data1); correlationTS = nan(N, 1); for t = 20+1:N correlationTS(t, :) = corr(Data1(t-20:t, 1),Data2(t-20:t,1),'rows','complete'); end y = correlationTS; end I am thinking that the for

How to deal with paired values?

混江龙づ霸主 提交于 2019-12-08 05:50:42
问题 Say I have a vector of independent variable values v =[ 1 2 2 1 1 .5 1 2 .5 .5 1] and a vector of response variables u = [ 5 22 20 4 8 .2 5 12 0 .5 6] I want to plot u vs. v with errorbars, the method needs to work for 100s of possible values for the independent variable. The problem isn't in plotting the error bars, it's in how can I create a vector pair [mean(u(find(v==0.5)), mean(u(find(v==1)), mean(u(find(v==2))] . Is there a standard automated way of doing this other than sorting v ,

How to deal with paired values?

社会主义新天地 提交于 2019-12-06 14:50:45
Say I have a vector of independent variable values v =[ 1 2 2 1 1 .5 1 2 .5 .5 1] and a vector of response variables u = [ 5 22 20 4 8 .2 5 12 0 .5 6] I want to plot u vs. v with errorbars, the method needs to work for 100s of possible values for the independent variable. The problem isn't in plotting the error bars, it's in how can I create a vector pair [mean(u(find(v==0.5)), mean(u(find(v==1)), mean(u(find(v==2))] . Is there a standard automated way of doing this other than sorting v , then picking out the values of sorted v and finding the index of v where v matches those values? This

Calculating a 2D joint probability distribution

和自甴很熟 提交于 2019-12-06 01:30:33
I have many points inside a square. I want to partition the square in many small rectangles and check how many points fall in each rectangle, i.e. I want to compute the joint probability distribution of the points. I am reporting a couple of common sense approaches, using loops and not very efficient: % Data N = 1e5; % number of points xy = rand(N, 2); % coordinates of points xy(randi(2*N, 100, 1)) = 0; % add some points on one side xy(randi(2*N, 100, 1)) = 1; % add some points on the other side xy(randi(N, 100, 1), :) = 0; % add some points on one corner xy(randi(N, 100, 1), :) = 1; % add