Subtract all elements in an array from each other consecutively - matlab

As the question says:
x = [1 2 3]
1 - 2 - 3 = -4
How do I get my -4?
Without a for-loop.
diff doesn't work. I don't get how Matlab has a way to SUM, but not a way to subtract.

It's an odd operation, I doubt there's a builtin function for that, but an easy way to do it would be:
2*x(1)-sum(x)

Another alternative is using dot product:
y = x*[1 ; -ones(numel(x)-1, 1)];
Here x is assumed a row vector.
Though probably not the best solution in your case, it can be nice say if you want other pattern for the summation, e.g. with weights - just replace the vector to the right by a vector of weights.

Related

how can I take a time reversal of an array and conv it in matlab

I have an h[n] = [1 1 1 -1 1] for [0:4] and I have h[-n] = [1 -1 1 1 1] for [-4:0]. so the question is since matlab index starts from 1, how do I make my array starts from 0, and how do I make the h[-n] to start from -4 so I can do convolution? please help, I am new to matlab!
The short answer is that you don't.
The long answer is that MATLAB has a very unique and specific manner of indexing and counting. The idea behind it is supposedly for faster and easier matrix manipulation by the user.
Given:
H = [1 1 1 -1 -1]
for all indices in order all you need to do is call
H[:]
If you want to flip the vector you should use the built in flip() command:
flip(H)
If you're attempting to iterate through a loop, then you want to use some sort of counting like:
for i=5:-1:1
...code...
I'd suggest reading up on array indexing as well as counting methods for loops to get a stronger understanding on the subject. These are some key points of MATLAB that you'll want to learn early if you plan on using it more in the future.

Matlab: Create a zero matrix with 1 in a particular place

For example, I can create a zeros(100). But I want the entry of row 58 and column 59 to be 1. But I need temporary variable and multiple lines to do this.
a. Let this matrix be M. How can I do this in one line? M = ....?
P.S.
b. Better still, sometimes I want two or more entries of the zero matrix be 1.
Again, how can I do this?
If I can do a. in one-line, of course I can add them up. But is there any special function to do fill zero matrix entries with 1?
First, remember that a one line expression isn't always the most effective. It could also be harder to read/understand.
One way to do this is by using a sparse matrix
The following example creates a 10x10 zero-matrix with ones at [5,2] (row 5, col 2) and [7 5]
full(sparse([5 7],[2 5],1,10,10))
Use full to convert it from a sparse matrix to a "full" one
Another (faster but maybe not as intuitive) alternative is to use accumarray
accumarray([5 2;7 5],1,[10,10])
Remember that the index values above is used directly in the expression to get on one line, the better option would be to create them separately
points = [5 2; 7 5]
or perhaps,
rowIdx = [5 7];
colIdx = [2 5];

What's the most idiomatic way to create a vector with a 1 at index i?

In Matlab, suppose I would like to create a 0-vector of length L, except with a 1 at index i?
For example, something like:
>> mostlyzeros(6, 3)
ans =
0 0 1 0 0 0
The purpose is so I can use it as a 'selection' vector which I'll multiply element-wise with another vector.
The simplest way I can think of is this:
a = (1:N)==m;
where N>=m. Having said that, if you want to use the resulting vector as a "selection vector", I don't know why you'd multiply two vectors elementwise, as I would expect that to be relatively slow and inefficient. If you want to get a vector containing only the m-th value of vector v in the m-th position, this would be a more straightforward method:
b = ((1:N)==m)*v(m);
Although the most natural method would have to be this:
b(N)=0;
b(m)=v(m);
assuming that b isn't defined before this (if b is defined, you need to use zeros rather than just assigning the Nth value as zero - it has been my experience that creating a zero vector or matrix that didn't exist before that is most easily done by assigning the last element of it to be zero - it's also useful for extending a matrix or vector).
I'm having a hard time thinking of anything more sensible than:
Vec = zeros(1, L);
Vec(i) = 1;
But I'd be happy to be proven wrong!
UPDATE: The one-liner solution provided by #GlenO is very neat! However, be aware that if efficiency is the chief criteria, then a few speed tests on my machine indicate that the simple method proposed in this answer and the other two answers is 3 or 4 times faster...
NEXT UPDATE: Ah! So that's what you mean by "selection vectors". #GlenO has given a good explanation of why for this operation a vector of ones and zeros is not idiomatic Matlab - however you choose to build it.
ps Try to avoid using i as a subscript, since it is actually a matlab function.
Just for the fun of it, another one-liner:
function [out] = mostlyzeros(idx, L)
out([L, idx]) = [0 1];
I can think of:
function mostlyones(m,n)
mat=zeros(1,m);
mat(n)=1;
Also, one thing to note. In MATLAB, index starts from one and not from zero. So your function call should have been mostlyzeros(6,3)
I would simply create a zero-vector and change whatever value you like to one:
function zeroWithOne(int numOfZeros, int pos)
a = zeros(numOfZeros,1);
a(pos) = 1;
Another one line option, which should be fast is:
vec = sparse(1, ii, 1, 1, L);

Binary operation with singleton expansion -- scalar output

There is such function as bsxfun: http://www.mathworks.com/help/techdoc/ref/bsxfun.html however it work in element-by-element mode. I want similar function which works in vector-by-vector mode (and with scalar output).
As illustration I would try to use here bsxfun in such way. As inner function I will use (this is just an example) dot product of vectors.
function f = foo(a,b), f=a'*b; printf("called\n");, end
The above dummy function foo expects 2 vector, the result is scalar. Each time it is called we will see a message.
bsxfun(#foo,[2;3],[1 5;4 3])
The result is:
called
called
ans =
14 19
0 0
So, two calls (nice), however instead of a vectors (pair of 2 scalars) we got a matrix. One can say, it will suffice to get just first row in such case, because the matrix is the created in advance by bsxfun, and the rest will be always zeros.
But it is not always a case -- sometimes I got some real values, not only zeros -- and I am afraid some side-effects are involved (the above dot product is the simplest example which came to head).
Question
So, is there a function similar to bsxfun, but which gets vectors and expects a scalar, per each operation of those vectors?
I don't think there is a built in function, but using arrayfun or cellfun you might be able to do something. Generally arrayfun is also element-wise, but if you first split your larger array into a cell then you can do it:
foo = #(a,b) b*a
y = [2;3];
X = [1 5; 4 3];
% split X into cell array of rows
% apply foo to each row
cellfun(#(x) foo(y,x), num2cell(X,2))
ans =
17
17
I am not sure it would give any speed advantage (I would imagine an explicit loop would be quicker) but sometimes it can be easier to read.

Using ranges in Matlab/Octave matrices

Let's say I want to create an 100x100 matrix of which every row
contains the elements 1-100
A = [1:100; 1:100; 1:100... n]
Obviously forming a matrix is a bad idea, because it would force me to
create 100 rows of range 1:100.
I think I could do it by taking a 'ones' array and multiplying every
row by a vector... but I'm not sure how to do it
a = (ones(100,100))*([])
??
Any tips?
You can use the repeat matrix function (repmat()). You code would then look like this:
A = repmat( 1:100, 100, 1 );
This means that you're repeating the first argument of repmat 100 times vertically and once horizontally (i.e. you leave it as is horizontally).
You could multiply a column vector of 100 1s with a row vector of 1:100.
ones(3,1)*(1:3)
ans =
1 2 3
1 2 3
1 2 3
Or you could use repmat ([edit] as Phonon wrote a few seconds before me [/edit]).
Yes, repmat is the easy solution, and even arguably the right solution. But knowing how to visualize your aim and how to create something that yields that aim will give long term benefits in MATLAB. So try other solutions. For example...
cumsum(ones(100),2)
bsxfun(#plus,zeros(100,1),1:100)
ones(100,1)*(1:100)
cell2mat(repmat({1:100},100,1))
and the boring
repmat(1:100,100,1)