Different For Loop Comparison

조회 수: 1 (최근 30일)
MrPuzzled Marc
MrPuzzled Marc 2011년 4월 27일
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?

채택된 답변

Matt Fig
Matt Fig 2011년 4월 27일
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
MrPuzzled Marc
MrPuzzled Marc 2011년 4월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by