vector which skip's a step - matlab

I want to create a vector without the number 1 .
x=-10:1:10;
To avoid this:
for(n=0:21)
if(x(n)==1)
x(n)=[];
end
end
What can I do ?

I would use setdiff
>> setdiff(-5:5,1)
ans =
-5 -4 -3 -2 -1 0 2 3 4 5

Instead of manually generating a vector from -10 to 10 and removing the entry that has the value of 1, you can always use colon / : and not include 1 in the vector instead. Something like:
x = [-10:0 2:10];
Because it's such a small vector, you probably won't gain much by doing it this way in comparison to fully generating the vector and removing one entry as per David's suggestion. I do agree with David though. Learn logical indexing! It's one of the backbones for making any MATLAB code fast.

You can try setting it manually to " ".
eg x(10)=[];

Related

Subtract all elements in an array from each other consecutively

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.

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.

read arrays in Simulink

I need some help of solving that issue: I have 5 different voltage values that change every single tick time - that mean every single moment. I need to sort them and after they been sorted I want to go to another matrix(like this one at the bottom) and to pull out(read) specific column from it, for every state pre define(timing that I am designing..) That mechanism change every single states/moment. How can I do this ?
The Matrix look like(and could be greater...):
0 0 0 1 1 1...
0 1 1 0 0 1...
1 0 1 0 1 0...
1 1 0 1 0 0...
.. .. .. .. .. ..
Thanks, Henry
I am not sure I understood it correctly. So I will edit my answer after you make your question a bit more clear.
I see two separate things:
Reading 5 voltage values which change at each step. You want to sort these values. To do this you can use the sort function of matlab. It is really easy to use and you can look at it here.
This is the part I didn't understand well. After sorting the voltage readings what do you want to do with the matrix ? If you want to access just a specific column of the matrix and save it in a variable you can do it in this way. Let's assume you have a matrix A which is N x N, if you want to access the 10th column of the matrix and store it in a variable called column10 you will do something like: column10 = A(:,10)
I hope this will help you but let me know if this is what you wanted and I will edit my answer according to it.
Fab.

replace zero values with previous non-zero values

I need a fast way in Matlab to do something like this (I am dealing with huge vectors, so a normal loop takes forever!):
from a vector like
[0 0 2 3 0 0 0 5 0 0 7 0]
I need to get this:
[NaN NaN 2 3 3 3 3 5 5 5 7 7]
Basically, each zero value is replaced with the value of the previous non-zero one. The first are NaN because there is no previous non-zero element
in the vector.
Try this, not sure about speed though. Got to run so explanation will have to come later if you need it:
interp1(1:nnz(A), A(A ~= 0), cumsum(A ~= 0), 'NearestNeighbor')
Try this (it uses the cummax function, introduced in R2014b):
i1 = x==0;
i2 = cummax((1:numel(x)).*~i1);
x(i1&i2) = x(i2(i3));
x(~i2) = NaN;
Just for reference, here are some similar/identical functions from exchange central and/or SO columns.
nearestpoint ,
try knnimpute function.
Or best of all, a function designed to do exactly your task:
repnan (obviously, first replace your zero values with NaN)
I had a similar problem once, and decided that the most effective way to deal with it is to write a mex file. The c++ loop is extremely trivial. After you'l figure out how to work with mex interface, it will be very easy.

What does the command A(~A) really do in matlab

I was looking to find the most efficient way to find the non zero minimum of a matrix and found this on a forum :
Let the data be a matrix A.
A(~A) = nan;
minNonZero = min(A);
This is very short and efficient (at least in number of code lines) but I don't understand what happens when we do this. I can't find any documentation about this since it's not an operation on matrices like +,-,\,... would be.
Could anyone explain me or give me a link or something that could help me understand what is done ?
Thank you !
It uses logical indexing
~ in Matlab is the not operator. When used on a double array, it finds all elements equal to zero. e.g.:
~[0 3 4 0]
Results in the logical matrix
[1 0 0 1]
i.e. it's a quick way to find all the zero elements
So if A = [0 3 4 0] then ~A = [1 0 0 1] so now A(~A) = A([1 0 0 1]). A([1 0 0 1]) uses logical indexing to only affect the elements that are true so in this case element 1 and element 4.
Finally A(~A) = NaN will replace all the elements in A that were equal to 0 with NaN which min ignores and thus you find the smallest non-zero element.
The code you provided:
A(~A) = NaN;
minNonZero = min(A);
Does the following:
Create a logical index
Apply the logical index on A
Change A, by assigning NaN values
Get the minimum of all values, while not including NaN values
Note that this leaves you with a changed A, which may be indesirable. But more importantly this has some inefficiencies as you spend time changing A and possibly even because you get the minimum of a large matrix.
Therefore you could speed things up (and even reduce one line) by doing:
minNonZero = min(A(logical(A)))
Basically you have now skipped step 3 and possibly reduced step 4.
Furthermore, you seem to get an additional small speedup by doing:
minNonZero = min(A(A~=0))
I don't have any good reason for this, but it seems like step 1 is now done more efficiently.