matlab to fortran translation help
2 views (last 30 days)
Show older comments
Hi
I'm converting some matlab code to fortran, mostly the syntax is similar however not sure what is happening here? (I get zeros, ones, unint8, length). I'm OK with the Fortran side, just not sure about certain aspects of matlab
What is the loop, [ ; ] etc doing and what's the difference between [ ; ] and [] ? numhids-1 = 14 by the way
if true
%%%%%%%%%%%
mm = uint8([0; 1]);
for jj=1:numhids-1
mm1 = uint8([zeros(length(mm),1); ones(length(mm),1)]);
mm = [mm; mm];
mm = [ mm1 mm];
end
%%%%%%%%%%%%%%%%%%%
% codeend
Rather than blindly translate I'd rather understand since sometimes there is an easier way to do it in fortran (or vice verce) ( I have matlab 14b by the way)
0 Comments
Accepted Answer
Guillaume
on 25 Oct 2014
Edited: Guillaume
on 25 Oct 2014
The ; concatenates values vertically (whereas , concatenates horizontally. Therefore mm starts out as a column of two values: 0 and 1.
From there, the loop duplicates the current mm vertically and append a column of 0s in front of the first half, and a column of 1s in front of the second half. Finally it uses this new array as the next mm and do that numhids - 1 times.
Your loop is basically generating all binary numbers of bit length numhids, with one bit per column and one number per row.
A much simpler way to achieve the same thing in matlab would have been the one liner:
mm = dec2bin(0:2^numhids-1, numhds) - '0';
I don't know anything about fortran, but in C, the fastest way to achieve the same thing would be with bit shifts.
0 Comments
More Answers (1)
See Also
Categories
Find more on Cell Arrays 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!