Calculate Interval mean of column in matlab (interval not fixed) - matlab

I have an array (2000x2) with two variables and want to calculate the mean of column 2 at intervals determined by column 1. How can i do this?
speed=(:,1); %values range from 0-100 cm/s
press=(:,2);
I want to calculate mean pressure at at 5 cm/s intervals of speed. So that I get 20 values for pressure that correspond to 20 intervals of speed.
Should be simple, but I'm still a beginner in Matlab.

The accumarray function does just that:
data = [0 20 33 44 22 56 25 47 81 90; 3 5 4 3 2 4 5 5 6 0].';
speed = data(:,1);
press = data(:,2);
sz = 5; % interval size
fill = NaN; % fill value in the result, for empty groups
group = floor(speed/sz)+1; % compute index of group for each value
result = accumarray(group, press, [], #mean, NaN); % compute mean of each group

Related

Calculating mean over column with condition

So my question is as follows: I have a matrix (let's take
A = [ 1 11 22 33; 44 13 12 33; 1 14 33 44,]
as an example) where I want to calculate the mean for all columns separately. The tricky part is that I only want to calculate the mean for those numbers in each column which are greater than the column 25th percentile.
I was thinking to simply create the 25th percentile and then use this as a criterion for selecting rows. This, unfortunately, does not work.
In order to further clarify: What should happen is to go through each column and calculate the 25th percentile
prctile(A,25,1)
And then calculating the mean only for those numbers which are respectively to their column bigger than the percentile.
Any help?
Thanks!
You can create a version of A which is NaN for values below the 25th percentile, then use the 'omitnan' flag in mean to exclude those points:
A = [1 11 22 33; 44 13 12 33; 1 14 33 44];
B = A; % copy to leave A unaltered
B( B <= prctile(B,25,1) ) = NaN; % Turn values to NaN which we want to exclude
C = mean( B, 1, 'omitnan' ); % Omit the NaN values from the caculation
% C >>
% [ 15.33 13.50 27.50 36.67 ]

Matlab - finding the values in a vector making a neighborhood chain

I have a vector that has values, say a=[10 20 42 90] and what I am trying to do is to find the neighbors in the range of 30 and replace these values with their means. For example, for the a vector, the value of 20 is a neighbor of 10. Additionally, 42 is also a neighbor of 10 through 20, because it is a neighbor's neighbor but 90 is not a neighboring value and it is not reachable from 10 with a neighborhood size of 30.
So I want to replace all 10, 20 and 42 with their means and obtain the vector a=[24 90].
If a=[10 20 42 66 155], then the resulting vector would be a=[34.5 155].
How do I achieve that?
a=[10 20 42 66 155]; % sample data
r = 30; % sample range
a = accumarray(cumsum([r+1 abs(diff(a))]>r).',a,[],#mean).';
Ungolfed and commented version:
a=[10 20 42 66 155]; % sample data
r = 30; % range
% difference between subsequent groupmembers. First difference is set to be higher than r
d = [r+1 abs(diff(a))];
% each group one label
L = cumsum(d>r);
% calculate mean of each group
a = accumarray(L.',a,[],#mean).';

to find maximum point from the graph/plot

a= [1 2 13 20 10 20 12 1 13 14]
b= [1:10:100]
plot(a,b)
I want to find the maximum('a') from the plot and then take the corresponding point let say 'a3,b3' and store it some where else and remove it from the plot. then I want to subtract 'a3' from every point left in 'a' and plot the graph. and I need to do this again till it reaches an thresh hold point.
This seems like an odd request but if I understand correctly.
%Input data
a = [1 2 13 20 10 20 12 1 13 14];
b = [1:10:100];
%Some threshold (which you didn't specify
lengthA = 4;
%Initialize storage vector
aPrime = a;
bPrime = b;
%While vector
while (length(aPrime) >= lengthA)
%New figure
figure;
%Plot vector
plot(aPrime, bPrime)
%Find index and max value in a'
[aMax, index] = max(aPrime);
%Find max value in b'
bMax = bPrime(index);
%Remove max value from vector and subtract off max value
aPrime = aPrime([1:index - 1, index + 1:end]) - aMax;
bPrime = bPrime([1:index - 1, index + 1:end]) - bMax;
end

How can I select random value that previously selected value are excluded from the selection

I want to select a random value but previously selected value are excluded from the selection. How can make like this??
Create a list of random numbers within the range, and pick one at a time
minimum=1;
maximum=16;
randomNumbers=randperm(maximum-minimum+1)+minimum-1
and sample output is randomNumbers=[7 13 2 15 12 4 16 11 9 5 3 1 14 6 8 10]. You can display each number sequentially using, for example,
for k=1:maximum+1-minimum
randomNumbers(k)
end
This approach is generalized one.. A could have any values, some numbers repeating or non repeating.. also any number of(of course upto numel(A)) could be generated.
Code:
A = randi(50,3,3); %// replace this with your own matrix
idx = 1:numel(A); %// generating linear indices
noOfRandNos = 5; %// How many such numbers do you want?
randNos(noOfRandNos,1) = 0; %// pre Allocation
%// This loop is run as many times as the number of such numbers you require.
%// Maximum possible runs will be equal to the numel(A).
for ii = 1:noOfRandNos
randidx = randi(numel(idx)); %// generating a rand Indx within the current size of idx
randNos(ii) = A(idx(randidx)); %// Getting corresponding number in-turn from the indx
idx(randidx) = []; %// removing the particular indx so that it is not repeated
end
Sample Run:
>> A
A =
12 28 25
23 27 32
49 12 34
>> randNos
randNos =
28
49
34
12
32
To pick 5 numbers from the set 1:16 without repetition: use randsample:
result = randsample(1:16, 5);

How to replace a block matrix with a new one?

I have an image size 128*128, I divided into 32*32 non-overlapping blocks, the problem is that I want to change the values of a specific or choosing block (the first or the 3rd.. for example) and getting a block with new values then replace it in my image. Do you have any idea how to get an image with ONE block modified ( NOT all of them)?
Thank you
This is an example with a small matrix
%*********************
A=magic(6) ; %%%%% matrix size 6*6
B=Block(A,2,2) ; % divide the matrix into 2*2 non-overlapping blocks
subblock=B{3} ; % Choose the 3rd block
new_block= 2*subblock; % change the block values by multipliying by 2
This is what I get
A = 35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 11
I extract the 3rd block
sub_block=19 24
23 25
Now I multiplied by 2 :
new_block= 38 48
46 50
This is my Block function:
function A=Block(IM,p,q)
p=3;
q=3;
[m,n] = size(IM);
IJ = zeros(p,q);
z = 1;
for m1 = 1:m/p
for n1 = 1:n/q
if m1*p <= m;
if n1*q <= n;
for i = (m1-1)*p+1:m1*p
for j = (n1-1)*q+1:n1*q
IJ(i-(m1-1)*p,j-(n1-1)*q) = IM(i,j);
if (i-(m1-1)*p)==p&&(j-(n1-1)*q)==q;
OUT = IJ;
A{1,z} = OUT;
z = z+1;
end
end
end
end
end
end
end
I want to replace these values in matrix A , but depending on block number. How can I do it?
Just type in the rows and cols you want to access, for your example
A(1:2,5:6)=A(1:2,5:6)*2
more generic, where n is the nth column block and m is the mth row block and c is the block width and r is the block height (in your example, n = 3, m = 1, r=c=2)
A(((m-1)*r+1):(m*r), ((n-1)*c+1):(n*c)) = any block of size(r,c)
I don't know about your Block function, you don't actually need to convert to a cell matrix but if you do want to then I would do this:
A = magic(6);
[m,n] = size(A);
r=2; %// Desired number rows of blocks, m must be a multiple of r
c=2; %// Desired number cols of blocks, n must be a multiple of c
%// Create blocks (but as a 2D grid rather than a list)
B = mat2cell(A,r*ones(m/r,1), c*ones(n/c,1))
%// Manipulate a block
B(1,3) = {2*B{1,3}};
%// Convert back to a matrix
cell2mat(B)
I think that RobertStettler's answer is the better way to go though, if this is all you are trying to do