Different For Loop Comparison
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!