Asked by Jacob Franklin
on 22 Aug 2019 at 21:25

Okay, suppose I have logical/TTL square wave array that looks something like this.

t = [0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0];

It is a scaled down representation of a much larger array with a 1:68608 scaling. I.e. here value 2 is at index 2 but in the larger array index 2 would start at 68608 + 1

I need to get the scaled indices of all the 0 and 1 sections for indexing the larger array. What I have been doing so far is getting the edge indices using strfind(t, [0 1]) and strfind(t, [1 0]) and multiplying them by the scaling factor to get the correct scaled indices for the edges and then some stuff with find(cumsum()) to get inclusive indices which gets me close by I am usually missing a unit or so because this wont include edges.

I just need a way to mathematically scale these indices. So instead of 6:10 and 16:20 I have 343041:686080 and 1029121:1372160 but in a way that will also include edges if for example I needed 1:5, 11:15, and 21 in which case 21 wouldn't just be 21 but 1372161:1440768.

Which has been the pit fall of my cum sum method so far as 21 just scales to 21 and doesnt give me a range except 21:21.

Edit to add a software example (ignore redunant logic, this was something I was playing with):

function [idx, numVals] = giveIndices(t)

valsPerUnit = 100;

numVals = valsPerUnit*numel(t);

starts = strfind(t, [0 1]);

stops = strfind(t, [1 0]);%+1;

if stops(1) < starts(1)

starts = [1 starts+1];

if starts(end) > stops(end)

if starts(end) < numel(t)

stops(end+1) = numel(t);%+1;

else

stops(end+1) = numel(t);

end

else

end

starts = starts*valsPerUnit;

starts = starts-(valsPerUnit-1);

stops = stops*valsPerUnit+1;

k = [];

k(starts) = -1;

k(stops) = 1;

idx = find(cumsum(k));

else

starts = starts*valsPerUnit+1;

stops = stops*valsPerUnit+1;

k = [];

k(starts) = -1;

k(stops) = 1;

idx = find(cumsum(k));

end

end

This will give you the correct indices for most circumstance but not always. For example this will work if t = [0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0] & t= [1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1], but will fail for t = [0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1] because it wont get the ends.

Answer by Stephen Cobeldick
on 23 Aug 2019 at 3:14

Edited by Stephen Cobeldick
on 23 Aug 2019 at 9:42

Accepted Answer

Here is one simple approach based on diff and find. Because a scaling factor of 68608 is rather inconvenient I used a scaling factor of 3.

>> t = [0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0]; % small vector.

>> k = 3; % scaling factor: large/small.

>> D = diff([false,t==1,false]); % small detect 1's.

>> B = k*(find(D>0)-1)+1 % large begin of 1's.

B =

16 46

>> E = k*(find(D<0)-2)+k % large end of 1's.

E =

30 60

>> z = kron(t,ones(1,k)) % large vector.

z =

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0

>> D = diff([false,z==1,false]); % large detect 1's.

>> find(D>0) % large begin of 1's.

ans =

16 46

>> find(D<0)-1 % large end of 1's.

ans =

30 60

Detecting the 0's I leave as a simple exercise for the reader.

Jacob Franklin
on 26 Aug 2019 at 15:01

I am accepting this as the answer because when given the correct inputs find(kron(t,ones(1,k))) does EXACTLY what I need.

My final solution formulates the scaled down logical array using a weighted mask and mod()

weightedMasked = [zeros(1, N)] ones(1, N)]; % N = 5 so [0 0 0 0 0 1 1 1 1 1];

elementNumbers = startNum:endNum; % Doing this in chunks so start of chunk to end of chunk

elementWeights = mod(elementNumbers, N*2) + 1; % +1 b/c index at 1 not 0 -> result is [1 2 3 4 5 6 7 8 9 10 1 2 4 5 6 7 8 9 10..etc]

scaledDownLogical = wieghtedMask(elementWeights);

scaledUpLogicalIdxs = find(kron(scaledDownLogica, ones(1, valsPerElement)));

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## David Hill (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/477249-how-to-scale-array-indices-to-larger-array-indices#comment_738138

## Jacob Franklin (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/477249-how-to-scale-array-indices-to-larger-array-indices#comment_738160

## David Hill (view profile)

## Direct link to this comment

https://es.mathworks.com/matlabcentral/answers/477249-how-to-scale-array-indices-to-larger-array-indices#comment_738182

Sign in to comment.