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.
Related
I want to sum elements of array which every element is a matrix.
I wrote below but not working:
AA={[1 2;3 4],[5 6;7 8]}
i=1:2;
sum(AA{i})
If you are wanting to perform operations across a set of 2-D matrices, all of which are the same size (and not too huge), it's easiest to store them as a 3-D matrix instead. See here for discussion/examples.
If you already have your matrices in a cell array, as in your example, you can concatenate them into a 3-D matrix using cat and sum across the third dimension without a for loop using sum:
mat = sum(cat(3, AA{:}), 3);
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{:});
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 to create a very big 3D matrix (such as: 500000x60x60). Is there any way to do this in matlab?
When I try
omega = zeros(500000,60,60,'single');
I get an out-of-memory error.
The sparse function is no option since it is only meant for 2D matrices. So is there any alternative to that for higher dimensional matrices?
Matlab only has support for sparse matrices (2D). For 3D tensors/arrays, you'll have to use a workaround. I can think of two:
linear indexing
cell arrays
Linear indexing
You can create a sparse vector like so:
A = spalloc(500000*60*60, 1, 100);
where the last entry (100) refers to the amount of non-zeros eventually to be assigned to A. If you know this amount beforehand it makes memory usage for A more efficient. If you don't know it beforehand just use some number close to it, it'll still work, but A can consume more memory in the end than it strictly needs to.
Then you can refer to elements as if it is a 3D array like so:
A(sub2ind(size(A), i,j,k))
where i, j and k are the indices to the 1st, 2nd and 3rd dimension, respectively.
Cell arrays
Create each 2D page in the 3D tensor/array as a cell array:
a = cellfun(#(x) spalloc(500000, 60, 100), cell(60,1), 'UniformOutput', false);
The same story goes for this last entry into spalloc. Then concatenate in 3D like so:
A = cat(3, a{:});
then you can refer to individual elements like so:
A{i,j,k}
where i, j and k are the indices to the 1st, 2nd and 3rd dimension, respectively.
Since your matrix is sparse, try to use ndsparse (N-dimensional sparse arrays FEX)
I am wondering if there is any build in function or an easy way to plot a histogram of elements of a 2d array.
For example, if A=rand(100,1), then A is an 1D array, and hist(A) can do the histogram.
However, what if A=rand(100,100), and I would like to make a histogram on elements of A, just like treating each element in A as an element on a 1D array. Is there a easy way to do so?
You just have to reshape A into a vector, then you can use hist as usual:
hist(A(:))
This command will do what you want:
hist(reshape(A, prod(size(A)), 1))
What it does is create a vector out of the matrix A by reshaping it into a matrix with one column and a number of rows equal to the number of elements of A:
prod(size(A)) = number_of_columns(A) * number_of_rows(A)
Or the short way:
hist(A(:))
This takes every element of A in sequence and thus also generates a vector.