I know how to take the mean of the whole image which is of size mxnx3 uint8 by using the following command
m = mean(I(:));
my understanding of this command is supposed we have a matrix
A=[1 2 3;4 5 6; 7 8 9];
mean_1=mean(A(:));
output is
A =
1 2 3
4 5 6
7 8 9
mean_1 =
5
A color image is stored as an mxnx3 matrix where each element is the RGB value of that particular pixel (hence it’s a 3D matrix). You can consider it as three 2D matrices for red, green and blue intensities.
so how mean is calculated in this case for three 2D matrices in Matlab?
As has been suggested in the comments, you could create a temporary array for the R, the G and the B pages of the matrix and calculate their mean, but in the specific case of a 3D RGB matrix you're probably better of just doing,
rgb_mean = squeeze(mean(mean(A,1),2))
If you're not familiar with squeeze, it will convert the 3D 1x1x3 matrix that results from taking the means, into a 2D 1x3 vector, which is most likely what you are expecting.
Related
I have generated a 3 x 2 x 25 matrix A in MATLAB.
A(1,2,1) = 5 means method 1, type 2, trial 1 has a count value of 5.
A(3,1,2) = 7 means method 3, type 1, trial 2 has a count value of 7.
Basically, there are 25 count values for each (method, type) pair.
In the past, I have used "histogram" MATLAB function to visualize a 2-D frequency plot and I know I can use it here like so:
histogram(A(3,1,:))
But if I use histogram I would have to plot all 6 like:
histogram(A(1,1,:))
histogram(A(1,2,:))
histogram(A(2,1,:))
histogram(A(2,2,:))
histogram(A(3,1,:))
histogram(A(3,2,:))
But I was wondering if there is a way to all 6 plots in a 3-dimensional histogram ?
You want 4 dimensions of information on a 3 dimensional plot. the only good way to get a 4th dimension of information would probably be color and that's going to be a mess with 6 plots. You're better off separating the plots into different graphs.
From what you're describing the second dimension only has 2 states. Would it make sense in your case to have 2 3d plots with this dimension being split across graphs/colors?
Part of an assignment of mine is to provide a 2D visualisation (plots) of some of my data points that are stored in a matrix. I'm slightly confused because the data is actually in 6D space (i.e. each row has 6 columns like 0 1 0 8 8 2).
Is there something I'm missing or does this genuinely not make sense? Is this something MATLAB can do?
Edit: Is something like this possible?
Though I wouldn't consider it visualizing 6D data, you can get the linked plot with a simple call to plot:
A = rand(6);
x = 1:6;
plot(x,A'); % Transpose A to plot rows since it's square, see plot documentation
Which produces the following:
From the documentation:
If one of X or Y is a vector and the other is a matrix, then the
matrix must have dimensions such that one of its dimensions equals the
vector length. If the number of matrix rows equals the vector length,
then the plot function plots each matrix column versus the vector. If
the number of matrix columns equals the vector length, then the
function plots each matrix row versus the vector. If the matrix is
square, then the function plots each column versus the vector.
Simply use:
surf(2Dmatrix)
You can read more here: http://uk.mathworks.com/help/matlab/ref/surf.html
If your matrix is 2D image, simply use
figure; imshow(2Dmatrix, [])
If you leave the square bracket empty, the limit will be automatic. When the figure is displayed you can change it to different colormap by Edit > Colormap.
I am building a 5-d tensor which explores a function in a 5-D space. The tensor is just the values of the function in discrete points in the space.
But, whenever I get the value, I want to get the indices of tensor / coordinate of space for later visualization. I tried to construct linspace-s for particular dimensions, then do a "sort of tensor exterior product". When I call the indices, I get the coordinate. It didn't work.
I hope I can get the indices with the value of the tensor at those indices, so I can rescale the indices into my coordinate. Is there such a function?
For example -
A =
1 2 3
4 5 6
7 8 9
For visualization, I want to plot A(:,2), a slice at fixed column. The coordinates are (1,2),(2,2),(3,2).
I want to plot f = (2,5,8) with coordinate = (1,2,3)
But now I have to generalize it into 5 dimensions and extract data for 2D colour plot (fix the other 3 dimensions)
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 want to convert a 2D matrix like A into a 3D matrix. Every slice should be the same content like this:
A=[1 2 3;4 5 6;7 8 9];
for i=1:10
B(:,:,i)=A
end
I need the same code without a loop, which decrease the speed of the program. In the original code A and i are rather big.
You can also try
B = A(:,:, ones(1,10) );
Running a small benchmark on ideone shows this approach significantly faster than bsxfun or repmat
I think the simplest and fastest way is using bsxfun, making use of the fact that it expands singleton dimensions:
A=[1 2 3;4 5 6;7 8 9];
B = bsxfun(#times,A,ones(3,3,10))
Here A is seen as a 3 x 3 x 1 matrix, and the third dimension is expanded to match the corresponding dimension in B (ie. 10).