Create Vector Ranges From Variables - matlab

I have two vectors that hold the "start" and "end" locations (as logicals) that I wish to combine as to create a third vector, Final:
Starts = [0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0];
Ends = [0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0];
With the Final Vector looking like this:
Final = [0 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 0];
Currently, I can accomplish this using a for loop as follows:
Start_Locations = find(Starts);
End_Locations = find(Ends);
Final = zeros(20,1);
for x=1:length(Start_Locations)
Final(Start_Locations(x):End_Locations(x),1) = 1;
end
I was wondering if there's a way to accomplish the same exact thing without a for loop. For example, I could accomplish what I outlined above with the following "hard-coded" statement:
Final([4:8,11:19],1) = 1;
Specifically, is there a way to combine the Start_Locations and End_Locations vectors in a way such that I could have a single statement like:
Final(Combined_Start_and_End_Locations,1) = 1;
to accomplish what I did with the for loop above? I'm trying to learn to avoid for loops as much as I can and would really appreciate any solution that creates the Final vector as described above without resorting to a loop.

Problems like this can often be solved using either diff or cumsum. They are essentially discrete derivative and integration functions.
For your problem I believe that
Final = cumsum([Starts 0]-[0 Ends]);
Final = Final(1:end-1);
achieves the desired result.

Related

Performing an averaging operation over every n elements in a vector

I have a logical vector in which I would like to iterate over every n-elements. If in any given window at least 50% are 1's, then I change every element to 1, else I keep as is and move to the next window. For example.
n = 4;
input = [0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 1];
output = func(input,4);
output = [0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1];
This function is trivial to implement but is it possible to apply a vectorized implementation using logical indexing?. I am trying to build up the intuition of applying this technique.
here's a one liner (that works for your input):
func = #(input,n) input | kron(sum(reshape(input ,n,[]))>=n/2,ones(1,n));
of course, there are cases to solve that this doesnt answer, what if the size of the input is not commensurate in n? etc...
i'm not sure if that's what you meant by vectorization, and I didnt benchmark it vs a for loop...
Here is one way of doing it. Once understood you can compact it in less lines but I'll details the intermediate steps for the sake of clarity.
%% The inputs
n = 4;
input = [0 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 1];
1) Split your input into blocks of size n (note that your final function will have to check that the number of elements in input is a integer multiple of n)
c = reshape(input,n,[]) ;
Gives you a matrix with your blocks organized in columns:
c =
0 0 0 0 0
0 1 0 1 0
0 1 0 0 0
1 0 1 1 1
2) Perform your test condition on each of the block. For this we'll take advantage that Matlab is working column wise for the sum function:
>> cr = sum(c) >= (n/2)
cr =
0 1 0 1 0
Now you have a logical vector cr containing as many elements as initial blocks. Each value is the result of the test condition over the block. The 0 blocks will be left unchanged, the 1 blocks will be forced to value 1.
3) Force 1 columns/block to value 1:
>> c(:,cr) = 1
c =
0 1 0 1 0
0 1 0 1 0
0 1 0 1 0
1 1 1 1 1
4) Now all is left is to unfold your matrix. You can do it several ways:
res = c(:) ; %% will give you a column vector
OR
>> res = reshape(c,1,[]) %% will give you a line vector
res =
0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 0 0 1

matlab transform logical index to range

Couldn't find a quick answer for this, it seems super simple but I don't get it. I want do the following transformation (for this example using my imaginary function transform):
a=[0 0 0 1 0 0 0 0];
b=(-1:2); %rule to transform for every true value in [a], -1:2 should be true
transform(a,b) %should output [0 0 1 1 1 1 0 0]
a=[0 0 0 1 0 0 0 0 1 1 0 0 0]; %another example
transform(a,b) %should output [0 0 1 1 1 1 0 1 1 1 1 1 0];
Is there an quick way of doing this transform, maybe using logical operators?
edit: I tried
a(find(a)'+(-1:2))=1 %requires matlab>2016 if I'm not mistaken, otherwise replace + sign with bsxfun(#plus,...)
but I'm looking for a possible function that does this without changing a and without using find (since using find kind of defeats the purpose of using logical matrices/indexing in the first place)
If you have the Image Processing Toolbox you can use imdilate:
nh(max(abs([b(1),b(end)]))+1+b) = true;
result = imdilate(a, nh);
I found an elegant oneliner that should solve your problem:
b=(-1:2)
a(find(a) + b(:)) = 1;
Hope it helps!

Generating in Matlab a "modified" diagonal matrix

I want to construct a matrix A in Matlab of dimension w x (m*w) where
each row is full of zeros except m consecutive ones that shift towards the right hand side as we move down to the rows.
Few examples can clarify
w=3,m=4
A=[1 1 1 1 0 0 0 0 0 0 0 0;
0 0 0 0 1 1 1 1 0 0 0 0;
0 0 0 0 0 0 0 0 1 1 1 1]
or
w=3, m=3
A=[1 1 1 0 0 0 0 0 0;
0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 1 1]
or
w=2, m=3
A=[1 1 1 0 0 0;
0 0 0 1 1 1]
I can't see how to proceed and any hint would be extremely helpful.
Step 1. Simplify the problem
If you write the "modified diagonal matrix" you are asking about as a row vector it will always look like the following
% 1 ... 1 0 ... ... 0 ... ... ... ... ... ... ... ... 1 ... 1
% m ones m*w zeros w-1 times the same as before m ones
Step 2. Think how to solve the simplified problem
The fundamental unit you need is a vector of m ones followed by m*w zeros;
Once you have built such vector, you need it to be repeated w times, MATLAB already knows how to do that;
The only thing you miss are the trailing ones: append them;
Now that the vector you were looking for is completed, you need to turn it into a matrix. MATLAB already knows how to do this too.
Final code
Once you understood the above steps, the final behaviour can be achieved even with a one-liner
>> m = 4; w = 3;
>> vec2mat([repmat([ones(1, m) zeros(1, m*w)], 1, w-1) ones(1, m)], w*m)
ans =
1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1
About speed
It's true, for loops aren't so slow anymore. I timed my one-liner solution, the trivial for loop and Luis Mendo's solution with eye() and repelem().
Click on images to zoom
Tested on the same machine, with MATLAB R2018a.
As you can see, as long as m and w are quite small, even if you could point out some differences in speed, them won't be noticeable to humans.
Anyway if you are going to work with bigger matrices, it becomes quite obvious which solution is the best.
Here are some approaches:
Using eye and repelem:
A = repelem(eye(w), 1, m);
Using eye and indexing:
A = eye(w);
A = A(1:w, ceil(1/m:1/m:w));
Using eye and kron:
A = kron(eye(w), ones(1,m));
Using singleton expansion:
A = bsxfun(#eq, (1:m).', ceil(1/m:1/m:w)); % Or A = (1:m).'==ceil(1/m:1/m:w);

How to solve an inconsistent system?

I'm new on MATLAB and I want to use it to solve an Ax = b system. I did this on paper and know I want to know if it's right. The problem is that it is an inconsistent system.
A=sym([3/sqrt(29) 3/sqrt(29) -1 0 0 0;
1 -1 0 0 0 0;
4/sqrt(29) 4/sqrt(29) 0 0 0 0;
0 0 1 9/sqrt(101) 0 0;
0 0 0 2/sqrt(101) -1/sqrt(5) 1/sqrt(5);
0 0 0 4/sqrt(101) 2/sqrt(5) 2/sqrt(5)])
c=sym([0 0 -a 0 0 -a])
When I tried finding the solution with:
A/c
I get:
Warning: The system is inconsistent. Solution does not exist.
I found many themes about that on the Internet, but there was no solution. Does this mean that means MATLAB can't handle it or is there a way to get a solution?
The system is unfortunately not being solved properly. You need to use the ldivide (\) operator, not rdivide (/). Doing A/c is equivalent to A*c^{-1} and that's not what you want. To solve for the solution to the system, you must do A^{-1}*c or equivalently A\c. Also, in order to ensure that you get a proper solution, c needs to be a column vector, not a row vector. I'm also assuming that a is a constant which isn't declared in the current code.
Therefore:
syms a; %// Added
A=sym([3/sqrt(29) 3/sqrt(29) -1 0 0 0;
1 -1 0 0 0 0;
4/sqrt(29) 4/sqrt(29) 0 0 0 0;
0 0 1 9/sqrt(101) 0 0;
0 0 0 2/sqrt(101) -1/sqrt(5) 1/sqrt(5);
0 0 0 4/sqrt(101) 2/sqrt(5) 2/sqrt(5)]);
c=sym([0 0 -a 0 0 -a]).'; %// Change
out = A\c;
I get:
out =
-(29^(1/2)*a)/8
-(29^(1/2)*a)/8
-(3*a)/4
(101^(1/2)*a)/12
-(5^(1/2)*a)/4
-(5*5^(1/2)*a)/12

Measure how spread out the data in an array is

I have an array of zeros and ones and I need to know if the data is spread out across the columns or concentrated in clumps.
For example:
If I have array x and it has these values:
Column 1 values: 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
Column 2 values: 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1
if we counted the number of ones we can know that it is the same number but the ones are more well spread out and distributed in column 2 compared with column 1.
I am trying to make a score that gives me a high value if the spreading is good and low value if the spreading is bad... any ideas??
Sample of Data:
1 0 0 0 5 0 -2 -3 0 0 1
1 0 0 0 0 0 0 0 0 0 1
2 0 0 0 0 0 0 3 -3 1 0
1 2 3 0 5 0 2 13 4 5 1
1 0 0 0 0 0 -4 34 0 0 1
I think what you're trying to measure is the variance of the distribution of the number of 0s between the 1s, i.e:
f = #(x)std(diff(find(x)))
So for you data:
a = [1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1]
b = [1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 1]
f(a)
= 8.0498
f(b)
= 2.0736
But I still think you're essentially trying to measure the disorder of the system which is what I imagine entropy measures but I don't know how
Note that this gives a low value if the "spreading" is good and a high value if it is bad (i.e. the opposite of your request).
Also if you want it per column then it becomes a little more complicated:
f = #(x)arrayfun(#(y)std(diff(find(x(:,y)))), 1:size(x,2))
data = [a', b'];
f(data)
WARNING: This method pretty much does not consider trailing and leading 0s. I don't know if that's a problem or not. but basically f([0; 0; 0; 1; 1; 1; 0; 0; 0]) returns 0 where as f([1; 0; 0; 1; 0; 1; 0; 0; 0]) returns a positive indicating (incorrectly) that first case is more distributed. One possible fix might be to prepend and append a row of ones to the matrix...
I think you would need an interval to find the "spreadness" locally, otherwise the sample 1 (which is named as Column 1 in the question) would appear as spread too between the 2nd and 3rd ones.
So, following that theory and assuming input_array to be the input array, you can try this approach -
intv = 10; %// Interval
diff_loc = diff(find(input_array))
spread_factor = sum(diff_loc(diff_loc<=intv)) %// desired output/score
For sample 1, spread_factor gives 4 and for sample 2 it is 23.
Another theory that you can employ would be if you assume an interval such that distance between consecutive ones must be greater than or equal to that interval. This theory would lead us to a code like this -
intv = 3; %// Interval
diff_loc = diff(find(input_array))
spread_factor = sum(diff_loc>=intv)
With this new approach - For sample 1, spread_factor is 1 and for sample 2 it is 5.