Replace zero values in vector - matlab

Ive got a vector like this
a=[0 5 3 0 1]
and a corresponding vector, containing the same amount of numbers as there are zeros in the first vector
b=[2 4]
what I want to get is
x=[2 5 3 4 1]
I tried fiddling around with, and somewhat got the feeling that the find / full methods might help me here, but didn't get it to work
c=(a==0)
>[1 0 0 1 0]
Thank you!

It is as easy as this:
x=a;
As x==0 gives the vector of all the locations an element = 0, ie [0 1 0 0 1], x(x==0) is indexing x to get the actual elements of x that are equal to 0, which you can then assign values as if it were any other vector/matrix (where the values we are not interested in do not exist, and are not indexed), using the following:
x(x==0)=b;

Related

Find Index of Zero or Almost Zero Rows

I have a NxM matrix with mixed rows
A = [[1.1 2.2 3.0]; [0.00000009 0 0]; [0 0 0]; [1 2 3]];
I want to find indices of all zero rows in A. From link I've tried
find(all(A==0,2))
and I am able to get the index of 3rd row i.e. [0 0 0] but not 2nd row which is also almost zero. How can i find all such rows which are either all zero or almost very near to zero.
I've used the following as work around but I don't think so its the correct way to solve this problem.
idx = unique([find(all(A<0.000001,2));find(all(A==0,2))]);
What is the correct way? Thanks for any help
First, define what you count as "almost very near to zero":
inc=1E-5;
Then, match the search to that criteria:
idx=find(all(abs(A)<inc,2))
The result is:
idx =
2
3

how to choose the range in histc? Why is there a 0 as indices?

i have a question regarding histc:
I choose the max and min of a sorted signal as my range.
ma = ssigPE(end);
mi = ssigPE(1);
range = mi:ma;
[bincountsO,indO2] = histc(ssigPE, range);
so the range i get back is:
range = [-1.097184703736132 -0.097184703736132 0.902815296263868]
my problem is that just 2 bins get develop, so bincountsO has 2 bins
and indO2 has values as 0, 1 and 2
What am I doing wrong? I guess I m using the range wrong. I read the text here:
http://de.mathworks.com/help/matlab/ref/histc.html#inputarg_binranges
but I don't get it.
The bin ranges tell you where do bins start and stop. So a value of [0 1 2 7]for example, will give 3 bins: [0 1] , [1 2] , [2 7]
In matlab if you do mi:ma it will create an array from the value mi to ma with a step of 1. With your values, that gives just 3 values, hence 2 bins. There are 2 ways of creating a given step size length vectors.
Step size if 100 as an example
range=mi:(ma-mi)/100:ma;
alternatively, and way clearer
range=linspace(mi,ma,100)

How to add vectors of different length in MATLAB

I'm trying to do the following:
Let's say I have:
x1=[7];
x2=[3 4];
x3=[1 -1 5];
x4=[2 5 -1 3];
and I want to add them together.
I know it's not possible to add vectors of different dimensions, but what I'm trying to achieve is a new vector having:
v=[2 5+1 -1-1+3 3+5+4+7];
I tried to pad the relevant vectors with zeros, to get:
x1=[0 0 0 7];
x2=[0 0 3 4];
x3=[0 1 -1 5];
x4=[2 5 -1 3];
and then the addition will be natural, but couldn't find a way to do it.
Of course, I'm looking for an iterative method of doing that, meaning, every vector xi is the result of the i'th iteration, where the number of iterations, n, is known in advance. (in the above example n=4)
My first thought would be something like
x1 = [zeros(1, 4 - length(x1)) x1];
Where you would substitute max(all_your_arrays) for 4 in the above line. If your arrays are in cell arrays you should be able to easily adapt that to a loop.

MATLAB syntax length

I'm reading some MATLAB trying to pick it up. The line below is probably rather simple but I do not understand it.
I understand length will give me the length of a vector, in this case a vector which is part of a struct, index_struct.data_incl.
The actual value of index_stuct.data_incl at run time is simply 1. What is confusing me is what is inside the brackets i.e. (index_struct.data_incl == 1)? I can't work out what this line is trying to do as simple as it may be!
int_var = length(index_struct.data_incl(index_struct.data_incl == 1));
try this (but think of x as your index_struct.data_incl:):
x = [1 4 5 13 1 1]
length(x(x==1))
ans =
3
It's just counting the number of elements of your x vector that are equal to 1
because x==1 evaluates to [1 0 0 0 1 1] and then using logical indexing x(x==1) evaluates to [1 1 1] whose length is 3;
It could have been written more simply as sum(index_struct.data_incl == 1)
If I dont see the code I can only guess..., but I guess that index_struc.data_incl should be a vector, with length n meaning that you have the option to read until n files, and all the values of the array should be 0 at the begining, and when you read a file you change the corresponding position in the vector index_struc.data_incl from 0 to 1. After some time you can see how many of these files you have read using
int_var = length(index_struct.data_incl(index_struct.data_incl == 1));
because it will give to you the number of 1 in the vector index_struct.data_incl.

Deleting columns in array with zeros

I have an array that starts of with zeros and continues into other numbers
I would like to delete the columns in the array that start off with zero but keep the other numbers
example of an column array below:
x= [0 0 0 0 0 2 4 6 8 0 1 2];
Answer of column array would look like
x= 2 4 6 8 0 1 2
I'm using octave 3.4.2/matlab
Thanks
Here is the code:
x = x(find(x~=0, 1):end);
or
x(1:find(x~=0,1)-1) = [];
The find command should work for this.
Assuming your vector is x:
find(x ~= 0)
Will return all indices where x is non-zero. Just grab the first index and go from there to delete all values from 1 to index.
Logical indexing will work just fine in this case: i.e.,
y = x(:,x(1,:)~=0)
will do the job for you. The inner logical comparison, x(1,:)~=0 returns true for every column whose first element is not zero. The indexing operation, x(:,...) selects only those columns for which the logical comparison returned true.