i have this code

1 次查看(过去 30 天)
Armina Petrean
Armina Petrean 2023-6-6
回答: Shivani 2023-6-27
i ahve this code and i donțt understand why is not working .
clear all
clc
close all
fid = fopen('corpusjuridicspatii.txt');
x=fread(fid,'*char');
binary = dec2bin(x,8);
binary_t=transpose(binary);
bin=binary_t(:)-'0';
i =1;
j =1;
while (i<length(binary_t))
cuvinte{j} = '';
while binary_t(i)~=32 % cat timp nu am dat de caracterul spatiu, concatenam caracterele la celula care contine cuvantul respectiv
cuvinte{j} = [cuvinte{j},char(binary_t(i))];
i = i+1;
end
j = j+1;
i = i+1; % cand dam de spatiu, sarim peste el ca sa nu ne blocam
end
[cuvinte_unice, ida, idb] = unique( cuvinte ) ;
counts = accumarray( idb, ones(size(idb)) ) ;
% cuvintele unice si numarul lor de ocurente
[ocurente_cuvinte_unice_sortate, indici_cuvinte_unice_sortate] = sort(counts,'descend');
frecvente_cuvinte_unice_sortate = ocurente_cuvinte_unice_sortate/length(cuvinte);
cuvinte_unice_sortate = cuvinte_unice(indici_cuvinte_unice_sortate);
for i = 1:length(cuvinte_unice_sortate)
['PROGRES DE ',num2str(i/length(cuvinte_unice_sortate)*100),' %']
cuvinte_numeric(find(strcmp(cuvinte,cuvinte_unice_sortate(i)))) = i;
% for j = 1:length(cuvinte_unice_sortate)
% if strcmp(cuvinte(i),cuvinte_unice_sortate(j))
% cuvinte_numeric(i) = j;
% break;
% end
% end
end
a=unique(cuvinte,'stable');
b=cellfun(@(x) sum(ismember(cuvinte,x)),a,'un',0);

回答(1 个)

Shivani
Shivani 2023-6-27
Hi Armina,
The error in the code is occurring because the while loop is not properly checking if the index i' exceeds the length of the binary_t array. This can cause the loop to run beyond the length of the array and result in the error message you are seeing.
To fix this error, you should add a check to ensure that i does not exceed the length of 'binary_t array. Here is an updated version of the code that should work:
clear all
clc
close all
fid = fopen('etxt1.txt');
x=fread(fid,'*char');
binary = dec2bin(x,8);
binary_t=transpose(binary);
bin=binary_t(:)-'0';
i =1;
j =1;
cuvinte{j} = '';
while (i<length(binary_t))
cuvinte{j} = '';
while (i<length(binary_t) && binary_t(i)~=32) % cat timp nu am dat de caracterul spatiu, concatenam caracterele la celula care contine cuvantul respectiv
cuvinte{j} = [cuvinte{j},char(binary_t(i))];
i = i+1;
end
j = j+1;
i = i+1; % cand dam de spatiu, sarim peste el ca sa nu ne blocam
end
Please note that I have also initialised the cuvinte variable before entering the loop to remove the warning shown in the attached screenshot. This warning appears because the length of this variable is varying at every iteration of the loop.
Hope this helps!

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!