Different For Loop Comparison
1 view (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Matt Fig
on 27 Apr 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))
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!