How to realize this in Matlab? - matlab

There is a matrix X, the size of which is 400-by-1000. I want to collect L = 10 samples, each of size M = 500, drawn without replacement from a uniform distribution over X. How to realize it by using Matlab? Anyone can give me a help?

The easiest thing would be to use randsample from the statistics toolbox. This allows you to take a random sample from a population / data without replacement. However, randsample takes in a vector, but you want to sample from a matrix. Judging from the context, each element in this matrix is equally likely to be chosen, so if we simply convert the matrix into a vector and we sample from this vector, you should achieve the same result.
If you want 10 samples of length 500, you can either call randsample once and get 5000 samples from the matrix, and reshape this into a 10 x 500 matrix where each row represents one 500 element sample, or loop this call 10 times and concatenate the results at each call.
So you can do this:
Y = randsample(X(:), 5000);
Y = reshape(Y, 10, 500);
The above code generates a 5000 x 1 vector and we reshape it into a 10 x 500 matrix.
Or you can do this:
Y = [];
for idx = 1 : 10
Y = [Y randsample(X(:), 500)];
end
Y = Y.';
randsample generates a 500 x 1 vector at each call, so we will stack these column vectors horizontally, then transpose the result so we get a 10 x 500 matrix.
However, if you don't have randsample, you can use randperm to generate a random permutation of numbers from 1 to 400 x 1000, and select 5000 elements from this permutation. You can then index into X to obtain your final matrix. Something like:
idx = randperm(400*1000, 5000);
Y = reshape(X(idx), 10, 500);

Related

What is the reverse process of the repmat or repemel command?

I have a matrix of 50-by-1 that is demodulated data. As this matrix has only one element in each row, I want to repeat this only element 16 times in each row so the matrix become 50 by 16. I did it using the repmat(A,16) command in Matlab. Now at receiving end noise is also added in matrix of 50 by 16. I want to get it back of 50 by 1 matrix. How can I do this?
I tried averaging of all rows but it is not a valid method. How can I know when an error is occurring in this process?
You are describing a problem of the form y = A * x + n, where y is the observed data, A is a known linear transform, and n is noise. The least squares estimate is the simplest estimate of the unknown vector x. The keys here are to express the repmat() function as a matrix and the observed data as a vector (i.e., a 50*16x1 vector rather than a 50x16 matrix).
x = 10 * rand(50,1); % 50x1 data vector;
A = repmat(eye(length(x)),[16,1]); % This stacks 16 replicas of x.
n = rand(50*16,1); % Noise
y = A * x + n; % Observed data
xhat = A \ y; % Least squares estimate of x.
As for what the inverse (what I assume you mean by 'reverse') of A is, it doesn't have one. If you look at its rank, you'll see it is only 50. The best you can do is to use its pseudoinverse, which is what the \ operator does.
I hope the above helps.

Calculate mean between 3D matrix

I have a matrix (100x50, it has random numbers) stored as x(:,:,1) and x(:,:,2). I want to calculate the average corresponding to the row and column of these matrixes but no luck so far. I tried to use the mean function but it gives me only one value. Any tips for an algorithm?
You can get for each matrix like the following:
mean(x(:,:,1),1) //avg in columns of x(:,:,1)
mean(x(:,:,1),2) //avg in row of x(:,:,1)
Also you can get the mean of x in different dimension using the following code:
mean(x,3); // size 100x50, avg of element of the two matrices
mean(x,2); // size 100 x 1 x 2, avg of rows of the two matrices
mean(x,1); // size 1 x 50 x 2, avg of columns of the two matrices

Calculate Euclidean distance for every row with every other row in a NxM matrix?

I have a matrix that I generate from a CSV file as follows:
X = xlsread('filename.csv');
I am looping through the matrix based on the number of records and I need to find the Euclidean distance for each of the rows of this matrix :
for i = 1:length(X)
j = X(:, [2:5])
end
The resulting matrix is of 150 X 4. What would be the best way to calculate the Euclidean distance of each row (with 4 columns as the data points) with every row and getting an average of the same?
In order to find the Euclidean distance between any pair of rows, you could use the function pdist.
X = randn(6, 4);
D = pdist(X,'euclidean');
res=mean(D);
The average is stored in res.

Sample 1D vectors from 3D array using a vector of points

I have a n channel image and I have a 100x2 matrix of points (in my case n is 20 but perhaps it is more clear to think of this as a 3 channel image). I need to sample the image at each point and get an nx100 array of these image points.
I know how to do this with a for loop:
for j = 1:100
samples(j,:) = image(points(j,1),points(j,2),:);
end
How would I vectorize this? I have tried
samples = image(points);
but this gives 200 samples of 20 channels. And if I try
samples = image(points,:);
this gives me 200 samples of 4800 channels. Even
samples = image(points(:,1),points(:,2));
gives me 100 x 100 samples of 20 (one for each possible combination of x in X and y in Y)
A concise way to do this would be to reshape your image so that you force your image that was [nRows, nCols, nChannels] to be [nRows*nCols, nChannels]. Then you can convert your points array into a linear index (using sub2ind) which will correspond to the new "combined" row index. Then to grab all channels, you can simply use the colon operator (:) for the second dimension which now represents the channels.
% Determine the new row index that will correspond to each point after we reshape it
sz = size(image);
inds = sub2ind(sz([1, 2]), points(:,2), points(:,1));
% Do the reshaping (i.e. flatten the first two dimensions)
reshaped_image = reshape(image, [], size(image, 3));
% Grab the pixels (rows) that we care about for all channels
newimage = reshaped_image(inds, :);
size(newimage)
100 20
Now you have the image sampled at the points you wanted for all channels.

3D matrix averaring in matlab

i have R 3d matrix,n varies from 1:100.
I have generated 20 such R matrix.
Now i have to average each R for this 20 experiment.
so that I'll get n,100 avg matrix.
How to average this 20, n Matrix?
I want to add(avg) all 20 times generated R for each n .I must have avg 100 R matrix .
Assuming you actually have a 3D matrix R, it is very easy to average:
R = rand(3,4,5); %Suppose this is your matrix
Now you just need to pick the dimension you want to average in:
mean(R,1) %First dimension
mean(R,2) %Second dimension
mean(R,3) %Third dimension
If you are not sure which one you need, just check the size of all three.