I want to calculate covariance of 400 50 X 50 matrices using loop so when I tried..
for i=t-9:t+59
for j=r-9:r+59
a(:,:,p)=vidFrames2(i:(i+49),j:(j+49));
b(:,:,p)=cov3d(double(a));
p=p+1;
end
end
this code I got error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
but when i want to remove this error by squeeze function in this code,
b(p,:)=cov3d(squeeze(double(a)));
I got "Subscripted assignment dimension mismatch."
how can I calculate covariance of 3d matrix....the cov3d function is-
function xy = cov3d(x)
[m,n,r] = size(x);
if m==1
x=zeros(n,n,r,class(x));
else
xc=bsxfun(#minus,x,sum(x,1)/m);
for i=1:r
xci=xc(:,:,i);
xy(:,:,i)=xci'*xci;
end
xy=xy/(m-1);
end
actually i wanted 400 number of covariance matrices stored in a variable.after this change i only get one covariance matrix.actually i am working on a frame where i am storing the initial coordinate values in i and j of a 50 X 50 matrix. then i want to get the pixel values of the 10 X 10 neighbourhood in the form of 50 X 50 matix. that means if i entered the initial coordinate as (100,100), then i want to get pixel values stored within region (91,91)(91,141)(141,91)(141,141).then store the covariance value of this matix. then again it will find the pixel values of the region (92,91),(142,91)(92,141),(142,141)and so on and covers the whole area..so if i cover the whole area then i think i will get total 400 matrices.and i want to store the covariance value of all the regions,.. today i changed the upper limit of the for loops to i=t-9:t+60 and j=r-9:r+60 but i did not get 400 covariance matrices
A few comments about your code:
cov3d is designed to return a 3D matrix, where the input is also a 3D matrix that calculates the covariance for each slice in this 3D matrix. From your code, the outer for loops (indices i and j) go from t-9 to t+59 in steps of 1 for each variable. As such, the pair of loops will execute ((t+59) - (t-9) + 1)^2 = 69^2 = 4761 times. I'm assuming that at each iteration, you are extracting a 2D matrix located at a which is why you initially said 400 times. You basically want to extract 400 matrices, but you are actually extracting 4761 matrices. Make sure a is properly declared to account for this many elements.
a(:,:,p)=vidFrames2(i:(i+49),j:(j+49)); accesses one slice, yet b(:,:,p)=cov3d(double(a)); is accessing the entire 3D matrix of a. Therefore, you will get a 3D matrix as the output of cov3d, yet you are trying to assign this to a single slice in b. What you should probably do is create your a matrix first, then make one call to cov3d when you're done.
Therefore, try changing your code to this:
%// Create `a` matrix first
for i=t-9:t+59
for j=r-9:r+59
a(:,:,p)=vidFrames2(i:(i+49),j:(j+49));
p=p+1;
end
end
%// Now call cov3d
b = cov3d(double(a));
Hopefully this will work the way you intended your code to originally work.
Related
Please correct me if there are somethings unclear in this question. I have two matrices pop, and ben of 3 dimensions. Call these dimensions as c,t,w . I want to repeat the exact same process I describe below for all of the c dimensions, without using a for loop as that is slow. For the discussion below, fix a value of the dimension c, to explain my thinking, later I will give a MWE. So when c is fixed I have a 2D matrix with dimension t,w.
Now I repeat the entire process (coming below!) for all of the w dimension.
If the value of u is zero, then I find the next non zero entry in this same t dimension. I save both this entry as well as the corresponding t index. If the value of u is non zero, I simply store this value and the corresponding t index. Call the index as i - note i would be of dimension (c,t,w). The last entry of every u(c,:,w) is guaranteed to be non zero.
Example if the u(c,:,w) vector is [ 3 0 4 2 0 1], then the corresponding i values are [1,3,3,4,6,6].
Now I take these entries and define a new 3d array of dimension (c,t,w) as follows. I take my B array and do the following what is not a correct syntax but to explain you: B(c,t,w)/u(c,i(c,t,w),w). Meaning I take the B values and divide it by the u values corresponding to the non zero indices of u from i that I computed.
For the above example, the denominator would be [3,4,4,2,1,1]. I hope that makes sense!!
QUESTION:
To do this, as this process simply repeats for all c, I can do a very fast vectorizable calculation for a single c. But for multiple c I do not know how to avoid the for loop. I don't knw how to do vectorizable calculations across dimensions.
Here is what I did, where c_size is the dimension of c.
for c=c_size:-1:1
uu=squeeze(pop(c,:,:)) ; % EXTRACT A 2D MATRIX FROM pop.
BB=squeeze(B(c,:,:)) ; % EXTRACT A 2D MATRIX FROM B
ii = nan(size(uu)); % Start with all nan values
[dum_row, ~] = find(uu); % Get row indices of non-zero values
ii(uu ~= 0) = dum_row; % Place row indices in locations of non-zero values
ii = cummin(ii, 1, 'reverse'); % Column-wise cumulative minimum, starting from bottomi
dum_i = ii+(time_size+1).*repmat(0:(scenario_size-1), time_size+1, 1); % Create linear index
ben(c,:,:) = BB(dum_i)./uu(dum_i);
i(c,:,:) = ii ;
clear dum_i dum_row uu BB ii
end
The central question is to avoid this for loop.
Related questions:
Vectorizable FIND function with if statement MATLAB
Efficiently finding non zero numbers from a large matrix
Vectorizable FIND function with if statement MATLAB
The 6 faces method is a very cheap and fast way to calibrate and accelerometer like my MPU6050, here a great description of the method.
I made 6 tests to calibrate the accelerometer based on the g vector.
After that i build up a matrix and in each row is stored the mean of each axis expressed in m/s^2, thanks to this question i automatically calculated the mean for each column in each file.
The tests were randomly performed, i tested all the six positions, but i didn't follow any path.
So i sorted manually the final matrix, based on the sort of the Y matrix, my reference matrix.
The Y elements are fixed.
The matrix manually sorted is the following
Here how i manually sorted the matrix
meanmatrix=[ax ay az];
mean1=meanmatrix(1,:);
mean2=meanmatrix(2,:);
mean3=meanmatrix(3,:);
mean4=meanmatrix(4,:);
mean5=meanmatrix(5,:);
mean6=meanmatrix(6,:);
meanmatrix= [mean1; mean3; mean2; mean4;mean6;mean5];
Based on the Y matrix constrain how can sort my matrix without knwowing "a priori" wich is the test stored in the row?
Assuming that the bias on the accelerometer is not huge, you can look at the rows of your matrix and see with which of the rows in your Y matrix matches.
sorted_meanmatrix = zeros(size(meanmatrix));
for rows = 1:length(Y)
% Calculates the square of distance and see which row has a nearest distcance
[~,index] = min(sum((meanmatrix - Y(rows,:)).^2, 2));
sorted_meanmatrix(rows,:) = meanmatrix(index,:);
end
I have two matrices. One contains the correct values of xyz normal vectors.
The second matrix contains estimated normal vector values. I need to know how different these vectors are from the other matrix.
Matrices are of the form:
[X1 Y1 Z1; X2 Y2 Z2; ...; Xn Yn Zn]
I was going to compute the absolute error, relative error, and root mean squared error.
I am relatively new to matlab, but I made an attempt with the following code:
abs_err = #(real, estimate) mae(estimate - real);
rel_err = #(real, estimate) sum(sum(abs((estimate-real)./real)));
rms_err = #(real, estimate) sqrt((sum(real(:)-estimate(:)).^2)/length(real(:)));
Where real and estimate are nx3 matrices.
I believe absolute is correct.
Relative error returns infinity with my dataset. Also, the inner sum produces a 1 row by 3 column matrix, but I'm not sure if summing those values is the correct way to get the error for a vector.
RMSE returns a fairly high value of 7.3, but I'm not sure if that is due to my data set or error in the calculation.
So, any advice on calculating these errors or where I'm going wrong? Thanks.
I have a 4D matrix with dimensions 7x4x24x10 (Lets call it main_mat). I want to get a matrix of size 7x4x24 (rand_mat) so that each element of this (rand_mat) matrix is actually a uniformly random draw from the main matrix (main_mat). I am sorry if I have not put the question clearly, so I try to explain:
I have a stack of 24 sheets of 7x4 elements, and I have 10 such stacks. What I want is that I get a single stack of 24 sheets of 7x4 elements in such a way that every element out of resultant single stack is uniformly randomly drawn from exactly same sheet number from within 10 stacks. How can I do it without using loops?
If I am interpreting what you want correctly, for each unique 3D position in this matrix of 7 x 4 x 24, you want to be sure that we randomly sample from one out of the 10 stacks that share the same 3D spatial position.
What I would recommend you do is generate random integers that are from 1 to 10 that is of size 7 x 4 x 24 long, then use sub2ind along with ndgrid. You can certainly use randi as you have alluded to in the comments.
We'd use ndgrid to generate a grid of 3D coordinates, then use the random integers we generated to access the fourth dimension. Given the fact that your 4D matrix is stored in A, do something like this:
rnd = randi(size(A,4), size(A,1), size(A,2), size(A,3));
[R,C,D] = ndgrid(1:size(A,1), 1:size(A,2), 1:size(A,3));
ind = sub2ind(size(A), R, C, D, rnd);
B = A(ind);
Bear in mind that the above code will work for any 4D matrix. The first line of code generates a 7 x 4 x 24 matrix of random integers between [1,10]. Next, we generate a 3D grid of spatial coordinates and then use sub2ind to generate column-major indices where we can sample from the matrix A in such a way where each unique 3D spatial location of the matrix A only samples from one chunk and only one chunk. We then use these column-major indices to sample from A to produce our output matrix B.
This problem might not be solvable without the use of loops. One way that could work is:
mainMatrix = ... (7x4x24x10 matrix)
randMatrix = zeros(mainMatrix(:,1,1,1), mainMatrix(1,:,1,1), mainMatrix(1,1,:,1))
for x = 1:length(mainMatrix(:,1,1,1))
for y = 1:length(mainMatrix(1,:,1,1))
for z = 1:length(mainMatrix(1,2,:,1))
randMatrix(x,y,z) = mainMatrix(x,y,z,randi(10))
end
end
end
I have a 3d matrix of 100x100x100. Each point of that matrix has assigned a value that corresponds to a certain signal strength. If I plot all the points the result is incomprehensible and requires horsepower to compute, due to the large amount of points that are painted.
The next picture examplify the problem (in that case the matrix was 50x50x50 for reducing the computation time):
[x,y,z] = meshgrid(1:50,1:50,1:50);
scatter3(x(:),y(:),z(:),5,strength(:),'filled')
I would like to plot only the highest values (for example, the top 10). How can I do it?
One simple solution that came up in my mind is to asign "nan" to the values higher than the treshold.
Even the results are nice I think that it must be a most elegant solution to fix it.
Reshape it into an nx1 vector. Sort that vector and take the first ten values.
num_of_rows = size(M,1)
V = reshape(M,num_of_rows,1);
sorted_V = sort(V,'descend');
ind = sorted_V(1:10)
I am assuming that M is your 3D matrix. This will give you your top ten values in your matrix and the respective index. The you can use ind2sub() to get the x,y,z.