How to make the same length of number?
4 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
Andrei Bobrov
2013-4-17
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 个评论
Jan
2013-4-17
@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!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Mobile Fundamentals 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!