Are there some nice way to do following.
I have 2 vectors where I want to only make sub vector multiplications. For examples,
a = 1:6; b = (1:6)'
Then I'd like the result:
result = [1*1+2*2+3*3; 4*4+5*5+6*6] = [14; 77]
So, I'd like to multiply each sub vectors of 3 element with each others. In the end, last element of the vector result would then be the sum or the result of a*b
Thank you in advance for your help
This can be done as
sum(reshape(a,3,[]).*reshape(b,3,[])).'
or
dot(reshape(a,3,[]),reshape(b,3,[])).'
maybe I'm missing something, but isn't it as simple as:
>> [a(1:3)*b(1:3) a(4:6)*b(4:6)]
ans =
14 77
??
Related
I am fairly new to matlab. I am sure there is a nice way to do this.
I have the vector h which contains 1257 elements.
And I have the vector t which contains 101 elements.
What I want is to assign the vector t from the 529th to the 630th element from vector h.
I tried this:
h(529:630) = t;
Then I get this message: "In an assignment A(I) = B, the number of elements in B and I must be the same."
If I use a scalar it works. For example:
h(529:630) = 5;
No problem there.
Can someone come up with something clever :)?
Thx
h(529:630) will have 630 - 529 + 1 = 102 elements
>> length(h(529:630))
ans =
102
You must use :
h(530:630) = t ;
Or
h(529:629) = t ;
whatever the case may be.
Hello I need to calculate a histogram for every row in a big matrix.
For the first row for example I get this:
AA = hist(symbolic_data(1,:), 1:8);
With symbolic_data(1,:)=[7 6 7 8 7], I get AA=[0 0 0 0 0 1 3 1].
Of course this is easy using a simple for loop, but my symbolic_data matrix is really big.
Is there a way to vectorize this.
I've been fiddling with bsxfun, but I can't make it work.
Any help would be much appreciated.
Thanks for your time.
From Matlab help:
N = hist(Y) bins the elements of Y into 10 equally spaced containers
and returns the number of elements in each container. If Y is a
matrix, hist works down the columns.
so:
AA = hist(symbolic_data', 1:8);
will do what you want
The answer by #Mercury is the way to go. But if you want to do it with bsxfun:
If you only have integer values, use
bin_centers = 1:8;
AA = squeeze(sum(bsxfun(#eq, permute(symbolic_data,[2 3 1]), bin_centers(:).')));
If the values are not necessarily integer:
bin_centers = 1:8;
AA = squeeze(sum( bsxfun(#le, permute(symbolic_data,[2 3 1]), bin_centers(:).'+.5) &...
bsxfun(#gt, permute(symbolic_data,[2 3 1]), bin_centers(:).'-.5) ));
I am trying to resolve why the following Matlab syntax does not work.
I have an array
A = [2 3 4 5 8 9...]
I wish to create an indexed cumulative, for example
s(1) = 2; s(2)=5, s(3)=9; ... and so on
Can someone please explain why the following does not work
x = 1:10
s(x) = sum(A(1:x))
The logic is that if a vector is created for s using x, why would not the sum function behave the same way? The above returns just the first element (2) for all x.
For calculating the cumulative sum, you should be using cumsum:
>> A = [2 3 4 5 8 9]
A =
2 3 4 5 8 9
>> cumsum(A)
ans =
2 5 9 14 22 31
The issue is that 1:x is 1 and that sum reduces linear arrays. To do this properly, you need a 2d array and then sum the rows:
s(x)=sum(triu(repmat(A,[prod(size(A)) 1])'))
You are asking two questions, really. One is - how do I compute the cumulative sum. #SouldEc's answer already shows how the cumsum function does that. Your other question is
Can someone please explain why the following does not work
x = 1:10
s(x) = sum(A(1:x))
It is reasonable - you think that the vector expansion should turn
1:x
into
1:1
1:2
1:3
1:4
etc. But in fact the arguments on either side of the colon operator must be scalars - they cannot be vectors themselves. I'm surprised that you say Matlab isn't throwing an error with your two lines of code - I would have expected that it would (I just tested this on Freemat, and it complained...)
So the more interesting question is - how would you create those vectors (if you didn't know about / want to use cumsum)?
Here, we could use arrayfun. It evaluates a function with an array as input element-by-element; this can be useful for a situation like this. So if we write
x = 1:10;
s = arrayfun(#(n)sum(A(1:n)), x);
This will loop over all values of x, substitute them into the function sum(A(1:n)), and voila - your problem is solved.
But really - the right answer is "use cumsum()"...
Actually what you are doing is
s(1:10)= sum(A(1:[1,2,3...10]))
what you should do is
for i=1:10
s(i)=sum(A(1:i))
end
hope it will help you
I have a vector A= [4 7 10] what I want to do is to sum every two elements and put the result in a new vector. So for example vector B= [11,17] which is the the sum of 4+7 and 7+10.
So if anyone could advise me how can I do this without loops.
In my view it is:
B = A(1:end-1) + A(2:end);
Here is an alternative that will be easy to generalize should you want to add groups of 3 or 4 etc in the future:
n = 2
conv(A, ones(1, n), 'valid')
You can do this
B = A(:,1:end-1) + A(:,2:end);
This code doesn't limit to just row vector. It will work on MxN matrix as well.
I have a file containing 60 matrices. I would like get the mean of each value across those 60 matrices.
so the mean of the [1,1] mean of [1,2] across the matrices.
I am unable to use the mean command and am not sure what's the best way to do this.
Here's the file: https://dl.dropbox.com/u/22681355/file.mat
You can try this:
% concatenate the contents of your cell array to a 100x100x60 matrix
c = cat(3, results_foptions{:});
% take the mean
thisMean = mean(c, 3);
To round to the nearest integer, you can use
roundedMean = round(thisMean);
You should put all the matrices together in a 3 dimensional (matrix?), mat, as:
mat(:,:,1) = mat1;
mat(:,:,2) = mat2;
mat(:,:,3) = mat3;
etc...
then simply:
mean(mat, 3);
where the parameter '3' stipulates that you want the mean accros the 3rd dimension.
The mean of the matrix can be computed a few different ways.
First you can compute the mean of each column and then compute the mean of those means:
colMeans = mean( A );
matMean = mean(colMean);
Or you can convert the matrix to a column vector and compute the mean directly
matMean = mean( A(:) );