I have generated a cell array of 1x5 cells with a 10x1 column vector on each cell.
How can I find the average of all the column vectors? So to have a final 10x1 new column vector that contains the means. Thank you
Here is an easy solution:
mean([A{:}]) % column-wise mean (1x5)
mean([A{:}],2) % row-wise mean (10x1)
I would concatenate the cell array contents along the second dimension (using cat and {:} indexing to get a comma separated list) and then take the mean along this first dimension
result = mean(cat(2, data{:}));
Since your data is purely numeric, you should avoid cell arrays and instead deal with a 5 x 10 matrix as that will be much more performant. You can easily create that the same way we did above.
matrix_data = cat(2, data{:});
Related
I'm looking for a way to select the vector that has the largest sum. Is there a simple way of doing this? I was thinking of writing a loop, but I'm not sure how to loop over a set of vectors.
Thanks for your help!
For the case in which the vectors have the same length (as stated in the comment), I think a simple loop-free way would be to build a matrix from each vector and fetch directly the row (or column) with the largest sum:
clear
clc
RandMat = rand(8,10);
[~,Ind] = max(sum(RandMat,2)); %// Get row index for largest sum. If you want the column, use 1 instead.
MaxRow = RandMat(Ind,:); %// Index in original matrix to get the vector. If you want the column, use RandMat(:,Ind);
If vectors don't have the same length then you would need to pad the missing values with NaN for example to use a regular matrix, otherwise you would need a cell array.
If you prefer a solution in which you don't have to build a matrix then you could loop through each individual vector and store the sum in a variable, then compare the sums at the end. If you would like such a solution please ask!
I am trying to add a number of vectors in a Matrix where each row represents a vector, but it gives me "Subscripted assignment dimension mismatch." error. The main problem is that each vector has a different size. I tried to add zeros at the end of the short vectors but I couldn't do it. Any Help.
Example:
%signal is a vector of data.
[x(1,:),y(1,:)] = findpeaks(signal1);
[x(2,:),y(2,:)] = findpeaks(signal2); %error as the peaks count in signal 2 is not the same as in signal 1.
OK, given two vectors of unequal length,
A=rand(1,10)
B=rand(1,5)
the proper way to deal with this is to use a cell array
D={A;B}
And then you can get whatever elements you want like this, for example:
D{1}(1:3) %// A(1:3)
If you don't want to use cells, you can add rows using this little function that adds row vector M to matrix F
addRow=#(F,M) [F NaN(size(F,1),size(M,2)-size(F,2));M NaN(1,size(F,2)-size(M,2))]
you would use it like this:
F=A
F=addRow(F,B)
I have this 3-D data in matlab of dimension 3x3x10. What I want is to reshape it to a data of size/dimension 10x9. Such that for each i,j,1:10. I have one column in this new data. How can I do it. I tried using reshape(data, 10,9). and it gave me a data structure of size 10x9. However, I doubt how it arranged it.
I want to reshape it just that if new_data is my new data of size 10x9, my first column is old_data(1,1,:). The second column of my new data is old_data(1,2,:) and so on
I personally abhor the use of for loops in Matlab. I think a better way would be to permute the dimensions, then reshape. Also, it is not clear the order in which you want to arrange the data. You should be able to achieve what you want by fiddling with the dimension numbers in the permute function call.
% B is a 3x3x10, permute to 10x3x3, then reshape into 10x9
% Change the ordering of the dimension in permute to achieve the desired result
% remember: reshape takes elements out of B column by column.
newB = reshape(permute(B,[3 1 2]),10,9);
% newB is a 10x9 matrix, where each row is the entries of the 3x3 matrix
% the first column is element 1:9:82
You could do this:
for i = 1:size(x,3)
x(:,:,i) = x(:,:,i).';
end
newData = reshape(x,[],size(x,3)).'
I have a single row matrix theta in Matlab with a couple hundred values; I would like to create a vector for each value in theta. Preferably I can simply store these values in a list so I can take dot products of individual elements in the list lator; how would I go about doing this?
For now, the code I have is
arrayfun(#(x) [sin(x),0,cos(x)],thetas,'UniformOutput',false);
which generates a bunch of [1x3 double]'s.
Instead of creating a cell array, you can also just create a numeric array of size numberOfThetas-by-3, like so:
A = [sin(thetas);zeros(size(thetas));cos(thetas)]'; %'# make n-by-3 by transposing
To calculate the dot product between any two vectors i and j, you can write
dotProduct = sum(A(i,:).*A(j,:));
You don't have to unnecessarily construct for loops to re-build the matrix as suggested by strictlyrude27. There is a nice inbuilt function cell2mat that does this for you in a single line.
Say your A is a 100 element cell, each of which holds a 1x3 vector. If you wanted to collect them such that each cell of A is a separate row, the command is simply
matrixA = cell2mat(A(:));
The output to your code is a cell array, whose elements are the 1x3 vectors you wanted. So suppose you assigned A = arrayfun(#(x) [sin(x),0,cos(x)],thetas,'UniformOutput',false); as you have up there. The vector corresponding with the ith element of array thetas may be accessed with A{i}. At this point you could use a for loop to construct a matrix whose ith column is the vector corresponding to the ith element of thetas.
Two questions:
1) I've found a piece of code that says something like cellArr{x}{y}{3,8} = 1.0;, I was wondering what the {3,8} means. The program connections various nodes in a collections of connected graphs together. Here we would say that "in the set of graphs x, the graph y's connection from 3 to 8 has a vertex label of 1.0". Still, in general what does the syntax {3,8} mean in MatLab?
2) This probably isn't the place for this question but should I really be using cell arrays if I know I'm always going to be having vertex values i.e. decimals/floats. Would a matrix be better because I know I'm only going to have a single data type?
Thank you :).
Cell arrays can have multiple dimensions, and thus they can be indexed with multiple subscripts like any other multidimensional array. The syntax {3,8} is indexing a (presumably) 2-D cell array, getting the contents of the cell in the third row and eighth column.
There are two main reasons to use cell arrays: storing data of different types or storing data of different sizes. Assuming x and y are scalar indices in your example, then cellArr is a cell array with the cell indexed by x containing another cell array, whose cell indexed by y contains a 2-D cell array which stores your vertex labels.
Now, if your vertex labels are all the same data type and are all just single non-empty (i.e. not []) values, then the 2-D cell array at the lowest level could be turned into a 2-D numeric array, and your indexing will look like this:
cellArr{x}{y}(3,8) = 1.0; %# Note the use of () instead of {}
The question now becomes how to handle the two enclosing sets of cell arrays indexed by x and y. If every cell that can be indexed by y contains 2-D numeric arrays all of the same size and type, then that cell array could be turned into a 3-D numeric array that could be indexed like so:
cellArr{x}(3,8,y) = 1.0; %# Here I've chosen to use y as the third dimension
Finally, if every cell that can be indexed by x contains 3-D numeric arrays that are again all of the same size and type, then cellArr could be turned into a 4-D numeric array that could be indexed like so:
numArr(3,8,y,x) = 1.0;
You could change the order of the subscripts (i.e. the dimensions of numArr) to your liking, but I put x and y at the end so that if you were to index a subarray of vertex labels like numArr(:,:,y,x) it will return it as a 2-D array. If you had the indices ordered such that you would index a subarray of vertex labels like numArr(x,y,:,:), it will return the result as a 4-D array with ones for the two leading singleton dimensions (which you would have to remove using functions like SQUEEZE).
The syntax {3,8} are cell array indices just as the {x} and {y} are. So cellArr is a cell vector of cell vectors. One of these cell vectors is indexed by {x}. This cell vector is itself a vector of cell 2d-matrices which is indexed by {y}. Finally, this cell matrix is indexed by {3,8}, i.e. the 3rd row and 8th column.
If all of your data is numeric then you'd be far better off using a 4 dimensional array. For your example, this array would be indexed by numericArray[x, y, 3, 8].