please help me convert matlab code to freemat

851 views
Skip to first unread message

Juckky

unread,
Nov 7, 2012, 3:40:53 AM11/7/12
to fre...@googlegroups.com

I'm new user for freemat and I learning to use freemat


freemat version 4.2 b

file name queen.m  

---------------------------------------------------------------------------------------

function queen

tic

clc;

counter = 0;

n = 8;

board = zeros(1,n);

[board,counter] = back(1, board,counter);

fprintf('Solution count: %d\n',counter);

toc

%%

function value = isSolution(board)

for i = 1:length(board)

for j = (i+1): length(board)

if abs(board(i) - board(j)) == abs(i-j)

value = false;

return;

end

end

end

value = true;

%%

function [board,counter] = back(depth, board,counter)

if (depth == length(board)+1) && isSolution(board)

counter = counter + 1;

disp(board);

end

if ( depth <= length(board))

for i = 1:length(board)

if ~any(board==i)

board(1,depth) = i;

[~,counter] = back(depth+1, board,counter);

end

end

end

--------------------------------------------------------------------------------------------------------------------

Error: unrecognized token at line number: 31 of file D:/Program File/FreeMat/queen.m

     board(1,depth) = i;

     [~,counter] = back(depth+1, board,counter);

       ^


question

1. in freemat What use for (~)

2. how can i use function (Error: only scripts can be source-ed, not functions)


sorry for my english. 

thank you  

Juckky

unread,
Nov 20, 2012, 7:45:06 AM11/20/12
to fre...@googlegroups.com
Now i can use fuction. but this error

Error: unrecognized token at line number: 31 of file D:/Program File/FreeMat/queen.m

     board(1,depth) = i;

     [~,counter] = back(depth+1, board,counter);

       ^

I can not solve it. in matlab and octave can execute successfully.

tisimst

unread,
Mar 20, 2013, 12:36:39 PM3/20/13
to fre...@googlegroups.com
Juckky,

I realize that this is a slightly older post and you might not care anymore, but here's a short answer anyway. The "~" syntax is for convenience that tells the Matlab/Octave interpreter that you don't care to store the object being returned by the function at that index. Since FreeMat doesn't support this, just put in a dummy variable that you know you'll never use like "FOO" and the function should work just fine. Hope that helps!

Regards,

Tisimst
Reply all
Reply to author
Forward
0 new messages