Different For Loop Comparison

1 vue (au cours des 30 derniers jours)
MrPuzzled Marc
MrPuzzled Marc le 27 Avr 2011
Does anyone can tell me is the output for this two loops is it the same? For my knowledge, the bits arranged are quite different in two case. will the bits differ in arrangement causes the file to be corrupted?
1) Without Preallocation send_count=1;
E=[];
for i=1+5000*(send_count-1):5000*send_count
E=[E F((-15+16*(i)):16*(i))];
end
2)Preallocation
send_count=1;
Idk=(1+5000*(send_count-1)):(5000*send_count);
C=zeros(16,(5000*looptimes));
for i=1:length(Idk)
C(:,i)=F((-15+16*(Idk(i))):16*(Idk(i)));
end
C=char(C);
send_count=1+send_count;
Does this give the same result?Actually I have tested it the bits arrangement using reshape are quite different. I m wondering will this cause the program to hang?

Réponse acceptée

Matt Fig
Matt Fig le 27 Avr 2011
Of course the values are not the same! C is a char but E is a double. Also, you say you reshaped C but you didn't do it in the code you show. That C and E could be equal (if they were the made to be the same size and data type) should not be too hard to prove to yourself, simply make a test...
For example, run this in a function M-file. Notice the ISEQUAL call at the end. If C and E are equal, it will say 1, if they are not, it will say 0. See for yourself!
looptimes = 1;
F = ceil(rand(1,5000000)*90); % Random data...
send_count = 1;
Idk = (1+5000*(send_count-1)):(5000*send_count);
%
%
% Make C.
%
%
C = zeros(16,(5000*looptimes));
for ii=1:length(Idk)
C(:,ii) = F((-15+16*(Idk(ii))):16*(Idk(ii)));
end
C = char(reshape(C,1,16*5000));
%
%
% Now make E.
%
%
E = [];
for ii=1+5000*(send_count-1):5000*send_count
E = [E F((-15+16*(ii)):16*(ii))];
end
isequal(C,char(E))
  1 commentaire
MrPuzzled Marc
MrPuzzled Marc le 27 Avr 2011
Yes, is the same.Sorry I did not see clearly when it reshape it. Sorry.
About this:
file=[];
received=fgetl(s);
file=[file received];
Do you have a way to preallocate this? I do not have for loop for this because I just received what ever from the serial port. After that when all the bits are gathered only i perform the next steps.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by