matlab to fortran translation help

2 views (last 30 days)
Steve
Steve on 25 Oct 2014
Edited: Steve on 26 Oct 2014
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)

Accepted Answer

Guillaume
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.

More Answers (1)

Steve
Steve on 25 Oct 2014
Thank you guys, I appreciate your time
I'll post my fortran translation as soon as I've checked it
regards Steve
  1 Comment
Steve
Steve on 26 Oct 2014
Edited: Steve on 26 Oct 2014
Hi
Thanks again, essentially what it was doing was creating a temp array of integers, filling them in a matrix 2^15-1 by 15 as binary representations ( I printed a few out to be sure).
Here is my solution in intel fortran which works - apologies can't get the hang of this sites code formatting ;( if true % code
Program Arraytest3
integer, dimension(15) :: buffer integer, dimension(0:32767,15) :: binaryMatrix integer column, row, N
! Fortran arrays start at 1 by default unless otherwise specified, to make row zero simpler have defined ! binaryMatrix to run from 0 to 2^15 - 1
binaryMatrix=0 ! 0 & 1 cases not divisible by 2 - initialise whole array, inc first row to zero binaryMatrix(1,15)= 1 ! 0 & 1 cases not divisible by 2 - initialise second row to 1 do row = 2, 2**15 N = row do column=1,15 buffer(column)= mod(N,2) N=N/2 enddo do column=15,1, -1 binaryMatrix(row,column) = buffer(16 - column) enddo enddo ! print it out do row = 0, 2**15-1 print*, binaryMatrix(row,:) enddo end Program Arraytest3 end

Sign in to comment.

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!