matlab fxn: find contiguous regions and return bounds in struct array - matlab

This is half a question and half a challenge to the matlab gurus out there:
I'd like to have a function take in a logical array (false/true) and give the beginning and ending of all the contiguous regions containing trues, in a struct array.
Something like this:
b = getBounds([1 0 0 1 1 1 0 0 0 1 1 0 0])
should return
b = 3x1 struct array with fields:
beg
end
and
>> b(2)
ans =
beg: 4
end: 6
I already have an implementation, but I don't really know how to deal with struct arrays well so I wanted to ask how you would do it - I have to go through mat2cell and deal, and when I have to deal with much larger struct arrays it becomes cumbersome. Mine looks like this:
df = diff([0 foo 0]);
a = find(df==1); l = numel(a);
a = mat2cell(a',ones(1,l))
[s(1:l).beg] = deal(a{:});
b = (find(df==-1)-1);
b = mat2cell(b',ones(1,l))
[s(1:l).end] = deal(b{:});

I don't see why you are using mat2cell, etc. You are making too much of the problem.
Given a boolean row vector V, find the beginning and end points of all groups of ones in the sequence.
V = [1 0 0 1 1 1 0 0 0 1 1 0 0];
You get most of it from diff. Thus
D = diff(V);
b.beg = 1 + find(D == 1);
This locates the beginning points of all groups of ones, EXCEPT for possibly the first group. So add a simple test.
if V(1)
b.beg = [1,b.beg];
end
Likewise, every group of ones must end before another begins. So just find the end points, again worrying about the last group if it will be missed.
b.end = find(D == -1);
if V(end)
b.end(end+1) = numel(V);
end
The result is as we expect.
b
b =
beg: [1 4 10]
end: [1 6 11]
In fact though, we can do all of this even more easily. A simple solution is to always append a zero to the beginning and end of V, before we do the diff. See how this works.
D = diff([0,V,0]);
b.beg = find(D == 1);
b.end = find(D == -1) - 1;
Again, the result is as expected.
b
b =
beg: [1 4 10]
end: [1 6 11]
By the way, I might avoid the use of end here, even as a structure field name. It is a bad habit to get into, using matlab keywords as variable names, even if they are only field names.

This is what I went with:
df = diff([0 foo 0]);
s = struct('on',num2cell(find(df==1)), ...
'off',num2cell(find(df==-1)-1));
I forgot about num2cell and the nice behavior of struct with cell arrays.

Related

Simplify a Matlab code involving finding the position of maximum elements of array

I would like your help to make more efficient (maybe, by vectorising) the Matlab code below. The code below does essentially the following: take a row vector A; consider the maximum elements of such a row vector and let, for example, be i and j their positions; construct two columns vectors, the first with all zeros but a 1 positioned at i, the second with all zeros but a 1 positioned at j.
This is my attempt with loops, but it looks more complicated than needed.
clear
rng default
A=[3 2 3];
max_idx=ismember(A,max(A));
vertex=cell(size(A,2),1);
for j=1:size(max_idx,2)
if max_idx(j)>0
position=find(max_idx(j));
vertex_temp=zeros(size(A,2),1);
vertex_temp(position)=1;
vertex{j}=vertex_temp;
else
vertex{j}=[];
end
end
vertex=vertex(~cellfun('isempty',vertex));
Still using a for loop, but more readable:
A = [3 2 3];
% find max indices
max_idx = find(A == max(A));
vertex = cell(numel(max_idx),1);
for k = 1:numel(max_idx)
vertex{k} = zeros(size(A,2),1); % init zeros
vertex{k}(max_idx(k)) = 1; % set value in vector to 1
end
If you really wanted to avoid a for loop, you could probably also use something like this:
A=[3 2 3];
max_idx = find(A==max(A));
outMat = zeros(numel(A), numel(max_idx));
outMat((0:(numel(max_idx)-1)) * numel(A) + max_idx) = 1;
then optionally, if you want them in separate cells rather than columns of a matrix:
outCell = mat2cell(outMat, numel(A), ones(1,numel(max_idx)))';
However, I think this might be less simple and readable than the existing answers.
Is there a specific reason you want a cell array rather than a matrix?
If you can have it all in one vector:
A = [3 2 3]
B_rowvec = A == max(A)
B_colvec = B_rowvec'
If you need them separated into separate vectors:
A = [3 2 3]
Nmaxval = sum(A==max(A))
outmat = zeros(length(A),Nmaxval)
for i = 1:Nmaxval
outmat(find(A==max(A),i),i)=1;
end
outvec1 = outmat(:,1)
outvec2 = outmat(:,2)
Basically, the second input for find will specify which satisfactory instance of the first input you want.
so therefore
example = [ 1 2 3 1 2 3 1 2 3 ]
first = find(example == 1, 1) % returns 1
second = find(example == 1, 2) % returns 4
third = find(example == 1, 3) % returns 7

For-loop exits too early, need help as to why

I've written a simple function that takes a vector vec, iterates through it, performing an operation whose result is stored in another vector vecRes of same size at same index, and returns vecRes upon completing the loop. Below is function code:
function [ vecRes ] = squareTerms( vec )
vecSize = size(vec);
vecRes = zeros(vecSize);
for i = 1:vecSize
vecRes(i) = vec(i)^2;
end
end
Problem is that it seems to exit too early, after only one iteration in fact as the output appears as:
vecRes = 1 0 0 0 0 0 0 0 0 0
For input:
vec = 1 2 3 4 5 6 7 8 9 10
I can't figure out why it does so. Any help is greatly appreciated.
Size returns 2 values, rows and columns. Probably you are a having a 1xN vector. So size returns [1 N] and your loop runs 1 time.
>>> size ([1 2 3])
>
> ans =
>
> 1 3
>
>>> 1:size ([1 2 3])
>
> ans =
>
> 1
Others have pointed out the problem. My preferred solution in this sort of case is to use numel, i.e.
vecRes = zeros(size(vec));
for i = 1:numel(vec)
vecRes(i) = vec(i) ^ 2;
end
Of course, in this case, vectorisation is better still:
vecRes = vec .^ 2;
Replace
for i = 1:vecSize
with
for i = 1:vecSize(2)
vecSize is an array of numbers, not just a single value. For example, if vec is a 1 by 8 vector, then size(vec) will return [1, 8].
Therefore, your for-loop-statement,
for i = 1:vecSize
, is actually equivalent to something like:
for i = 1:[1, 8]
This doesn't make much sense. There are a number of ways to fix the problem. You could write:
for i = 1:length(vec)
or
for i = 1:numel(vec) % "numel" stands for "number of elements"
If the vector is 1xn instead of nx1, you can write:
for i = 1:size(vec, 2)
Yet another alternative is:
for i = 1:max(vecSize)
However, the most sensible option is not to write the squareTerms function at all and simply write
vecRes = vec.^2;
Note the dot before the caret. vec^2 and vec . ^2 are not the same thing.
If you put a dot before an operator sign, the operation will be performed element-wise. For example,
C = A * B
performs matrix multiplication, but
C = A .* B
will cause the first element of A to by multiplied by the first element of B, and the result will be assigned to the first element of C. Then, the product of the second elements of A and B will be taken, and the result will be stuck in the second element of C, and so on.
vecRes = vec.^2;

Find number of consecutive elements before value changes (MATLAB)

I have a (row)vector of some size, containing the values 1,2 and 3. They are in there in no 'specific' order, so a sample of the array would be [1,1,1,1,2,2,2,1,1,2,2,3,3]. What I want to do is find the consecutive number of identical elements, but with some restrictions.
What I want is to make new arrays of which the elements denote:
The number of consecutive 1's before it changes into a 2
The number of consecutive 2's before it changes into a 1
The number of consecutive 2's before it changes into a 3
The number of consecutive 3's before it changes into a 2
So for the example I have given, the arrays would be
[4,2]
[3]
[2]
[]
I'm not sure how to tackle this. I can use the diff function to find where it changes sign, but then it'll be a little tough to figure out exactly what change has occured, right?
The method does not have to be super fast, as I only have to do this a few times for around 10^5 datapoints.
This approach will group things the way you specified in the question:
a=[1,1,1,1,2,2,2,1,1,2,2,3,3]
b = diff(a)
c = find(b)
d = diff([0,c]);
type1 = d(b(c) == 1 & a(c) == 1);
type2 = d(b(c) == -1 & a(c) == 2);
type3 = d(b(c) == 1 & a(c) == 2);
type4 = d(b(c) == -1 & a(c) == 3);
type1 =
4 2
type2 =
3
type3 =
2
type4 =
Empty matrix: 1-by-0
Use the standard procedure with diff to detect changes and run lengths, and then apply accumarray to group run lengths according to each pair of values before and after the change:
x = [1,1,1,1,2,2,2,1,1,2,2,3,3];
x = x.'; %'// easier to work with a column vector
ind = find(diff(x))+1; %// index of positions where x changes
%// To detect what change has ocurred, we'll use x(ind-1) and x(ind)
len = diff([1; ind]); %// length of each run
result = accumarray([x(ind-1) x(ind)], len, [], #(v) {v}); %// group lengths
Note the order within each result vector may be altered, as per accumarray.
In your example, this gives
>> result
result =
[] [2x1 double] []
[3] [] [2]
>> result{1,2}
ans =
2
4
>> result{2,1}
ans =
3
>> result{2,3}
ans =
2
I believe this will do the trick (although it's not very pretty)
a=[1,1,1,1,1,2,2,2,2,1,1,1,2,2,3,3,3];
d=diff(a);
deltas=(d~=0);
d12=[];d23=[];d32=[];d21=[];
last=0;
for i=1:length(a)-1
if deltas(i)
if a(i)==1&&a(i+1)==2
d12=[d12,i-last];
last=i;
elseif a(i)==2&&a(i+1)==3
d23=[d23,i-last];
last=i;
elseif a(i)==3&&a(i+1)==2
d32=[d32,i-last];
last=i;
elseif a(i)==2&&a(i+1)==1
d21=[d21,i-last];
last=i;
end
end
end

Two FOR statements coupled into one

Is is possible to put two for statements into one statement. Something like
A = [ 0 0 0 5
0 2 0 0
1 3 0 0
0 0 4 0];
a=size(A);
b=size(A);
ind=0;
c=0;
for ({i=1:a},{j=1:b})
end
Your question is very broad, but one thing to consider is that in MATLAB you can often take advantage of linear indexing (instead of subscripting), without actually having to reshape the array. For example,
>> A = [ 0 0 0 5
0 2 0 0
1 3 0 0
0 0 4 0];
>> A(3,2)
ans =
3
>> A(7) % A(3+(2-1)*size(A,1))
ans =
3
You can often use this to your advantage in a for loop over all the elements:
for ii=1:numel(A),
A(ii) = A(ii) + 1; % or something more useful
end
Is the same as:
for ii=1:size(A,2),
for jj=1:size(A,1),
A(jj,ii) = A(jj,ii) + 1;
end
end
But to address your specific goal in this problem, as you stated in the comments ("I am storing the non zero elements in another matrix; with elements like the index number, value, row number and column number."), of making sparse matrix representation, it comes to this:
>> [i,j,s] = find(A);
>> [m,n] = size(A);
>> S = sparse(i,j,s,m,n)
S =
(3,1) 1
(2,2) 2
(3,2) 3
(4,3) 4
(1,4) 5
But that's not really relevant to the broader question.
Actually you can combine multiple loops into one for, however it would require you to loop over a vector containing all elements rather than the individual elements.
Here is a way to do it:
iRange = 1:2;
jRange = 1:3;
[iL jL] = ndgrid(iRange,jRange);
ijRange = [iL(:) jL(:)]';
for ij = ijRange
i = ij(1); j = ij(2);
end
Note that looping over the variables may be simpler, but perhaps this method has some advantages as well.
No
read this http://www.mathworks.com/help/matlab/matlab_prog/loop-control-statements.html
i also don't see any added value even if it was possible
No I don't think you can put two for loops in one line.
Depends on your operation, you may be able to reshape it and use one for loop. If you are doing something as simple as just printing out all elements,
B = reshape(A,a*b,1);
for i=1:a*b
c = B(i);
...
end

haltonset: understanding skip and leap

If i read the doc of how to construct a Halton quasi-random point set and it mentions that it's possible to 'skip' the first values and then retain the 'leap' values.
Don't understand what the 'skip' and 'leap' really mean.
Have tried the following:
>> p = haltonset(1,'Skip',50,'Leap',10); d = haltonset(1,'Skip',51,'Leap',9);
>> p(2:10), d(1:9)
ans =
0.7344
0.0703
0.7891
0.4766
0.5859
0.1797
0.9922
0.3164
0.6602
ans =
0.7969
0.7344
0.8828
0.5391
0.8516
0.6484
0.9609
0.6172
0.7539
>> p(2:10) == d(1:9)
ans =
0
0
0
0
0
0
0
0
0
Thought that it might be that that this would save 10 values to p and 9 to d. Also thought that d would have the same values as p. But this was not the case.
I then tested if the 'leap' would be the same as a normal way to make a vector
- ex: (1:leap:10)
>> p = haltonset(1,'Skip',50,'Leap',1); d = haltonset(1,'Skip',50,'Leap',2);
>> p(1:2:10)==d(1:5)
ans =
1
0
0
0
0
>> p = haltonset(1,'Skip',0,'Leap',1); d = haltonset(1,'Skip',0,'Leap',2);
>> p(1:2:10)==d(1:5)
ans =
1
0
0
0
0
but this seemed not to be the case..
Can anybody give a plain English explanation of how to interpreted the 'skip' and 'leap' variables.
I find the following description to be very clear [quoting this documentation page]:
Imagine a simple 1-D sequence that produces the integers from 1 to 10.
This is the basic sequence and the first three points are [1,2,3]:
Now look at how Scramble, Leap, and Skip work together:
Scramble: Scrambling shuffles the points in one of several
different ways. In this example, assume a scramble turns the sequence
into 1,3,5,7,9,2,4,6,8,10. The first three points are now [1,3,5]:
Skip: A Skip value specifies the number of initial points to
ignore. In this example, set the Skip value to 2. The sequence is now
5,7,9,2,4,6,8,10 and the first three points are [5,7,9]:
Leap: A Leap value specifies the number of points to ignore for
each one you take. Continuing the example with the Skip set to 2, if
you set the Leap to 1, the sequence uses every other point. In this
example, the sequence is now 5,9,4,8 and the first three points are
[5,9,4]:
EDIT:
Let me show with an example:
%# create 1D sequences (x: picked, .: ignored)
p00 = haltonset(1,'Skip',0,'Leap',0); %# xxxxxxxxxxxxxxx
p50 = haltonset(1,'Skip',5,'Leap',0); %# .....xxxxxxxxxx
p02 = haltonset(1,'Skip',0,'Leap',2); %# x..x..x..x..x..
p52 = haltonset(1,'Skip',5,'Leap',2); %# .....x..x..x..x
%# each pair of these are equal
[p50(1:10) p00(6:15)] %# skip vs. noskip
[p02(1:5) p00(1:3:13)] %# leap vs. noleap
[p52(1:4) p00(6:3:15)] %# skip+leap vs. noskip+noleap
In general:
skip = 50;
leap = 10;
p00 = haltonset(1,'Skip',0,'Leap',0);
p = haltonset(1,'Skip',skip,'Leap',leap);
num = 9;
[p(1:num) p00(skip+1:leap+1:num*leap+num-leap+skip)]