How to Scale Array Indices to larger array indices?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jacob Franklin
am 22 Aug. 2019
Kommentiert: Jacob Franklin
am 26 Aug. 2019
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.
3 Kommentare
David Hill
am 23 Aug. 2019
idx=find(abs(diff(t))+1;
idx=[idx,length(t)+1];
scaled_idx=scaling*idx;
finalMatrix=[];
Length=1;
for i=1:length(idx)
if t(idx(i)-1)
finalMatrix=[finalMatrix,ones(1,scaled_idx(i)-Length)];
else
finalMatrix=[finalMatrix,zeros(1,scaled_idx(i)-Length)];
end
Length=scaled_idx(i);
end
I think the above will give you what you want.
Akzeptierte Antwort
Stephen23
am 23 Aug. 2019
Bearbeitet: Stephen23
am 23 Aug. 2019
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.
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!