How to make the same length of number?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
i have numbers, its size are different each other. e.g 2 3 1 1 2 2 3 3 3, 2 2 3 2 2 ,2 2 2 2 3 3 3 1 1 1 1 1,so on. i wanna to make them have the same length. this is my code
gb=[2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 3 1 2 2 2 2 2 2 3 2 2 2 3 2 2 2 3 2 3 2 2 2 3 2 3 2 3 2 2 2 2 2 2 2 2 2 3 2 2 1 3 2 2 1 2 2 2 2 2 2 2 2 2 1 3 1 2 1 3 2 2 1 3 2 2 2 2 2 2 2 2 2 3 2 1 2 2 3 2 1 3 1 2 1 3 1 2 3 2 2 3 1 2 1 3 2 2 1 2 2 3 2 2 1 3 3 2 1 3 2 2 3 3 2 3 2 2 1 3 2 2 1 2 2 2 2 2 2 2 2 3 2 1 1 3 2 2 1 3 2 1 3 2 2 3 2 1 1 3 3 1 3 2 1 1 3 3 1 1 3 3 1 3 2 1 3 2 1 2 2 3 3 3 2 2 3 2 3 2 2 3 2 2 2 3 2 2 3 1 3 2 3 1 3 2 3 1 3 2 3 2 2 2 3 2 2 2 3 2 3 1 3 2 2 1 3 2 2 2 3 1 2 1 3 2 3 1 2 2 3 2 3 1 3 1 2 2 2 1 2 2 2 2 3 1 2 2 3 2 3 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 2 3 1 2 1 3 2 2 1 2 2 2 2 2 1 2 1 2 2 3 1 2 2 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
]
b = gb([diff(gb)~=0, true]);
f = diff(find([true,diff(gb)~=0,true]));
g=b(f~=1);
h=f(f~=1);
[a,b]=unique(g);
new_g=g(sort(b));
new_h=arrayfun(@(x) sum(h(g==x)),new_g);
normalized_frequency=[];
for i=1:length(new_h)
result=(new_h(i)/sum(new_h))*64;
normalized_frequency=[normalized_frequency result];
end
normalized_frequency;
rounding=round(normalized_frequency);
duplicate=[];
for i=1:length(new_g)
duplicate=[duplicate repmat(new_g(i),1,rounding(i))];
end
duplicate;
gb is an array which wanna be converted into length 64. maybe if you try another number, you'll get the length as you want, but here i give another example that won't get the right length. in this case gb has length 65 not 64 as i want. What should i do? please help me? Thanks in advance.
5 Kommentare
Antworten (1)
Andrei Bobrov
am 17 Apr. 2013
ii = [true,diff(gb)~=0];
g = [gb(ii);diff(find([ii,true]))];
g1 = g(:,g(2,:)>1);
[a,b,c] = unique(g1(1,:));
w = accumarray(c,g1(2,:)');
f = [a;w.'];
[~,i1] = sort(b);
f = f(:,i1);
f(2,:) = cumsum(f(2,:)/sum(f(2,:)));
l = linspace(0,1,64);
[~,j2] = histc(l,[0 f(2,:)+eps(100)]);
out = f(1,j2);
1 Kommentar
Jan
am 17 Apr. 2013
@Andrei: This is one of the answers with the highest complexity in this forum. I do not understand what the program performs instantly. Although I cannot test it by my own, I'm convinced that it earns a vote!
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!