Hi there.
I've been solving PE problems using MATLAB, and in most cases have been able to enter the data files with no problem as MATLAB reads them either as a vector or a matrix. However, in the case of Prob18 the data is a triangular array; as expected MATLAB doesn't know what to do with this as it wants to treat it like a matrix. Does anybody know how I can get around this without having to append zeros to each row!
Thank you.
Entering Array in MATLAB (eg. Prob18)
Re: Entering Array in MATLAB (eg. Prob18)
You may wish to consider cell arrays http://www.advancedmcode.org/strings-ce ... atlab.html.
Simply change the outside [] to {} and you can write things like:
A={[1],[1,2],[1,2,3]};
Simply change the outside [] to {} and you can write things like:
A={[1],[1,2],[1,2,3]};
Re: Entering Array in MATLAB (eg. Prob18)
To use cell arrays you might want to try a function such as:
You can then use this by doing things like:
This will print out the first element in the second row of the input.
(Note that the first brackets are {}, and the second (). This is because A is a cell array, but the content of a cell is a normal Matlab array)
Code: Select all
function A = readtri ()
fid=fopen('triangle.txt');
A={};
n=1;
while(~feof(fid))
A(n)=fscanf(fid,'%d',n);
n=n+1;
end
endfunction
Code: Select all
A=readtri();
disp(A{2}(1))
(Note that the first brackets are {}, and the second (). This is because A is a cell array, but the content of a cell is a normal Matlab array)
Re: Entering Array in MATLAB (eg. Prob18)
To avoid cell arrays and just get Matlab to enter the zeros for you, you could use a function like this:
You can then use normal Matlab A(2,1) to access the first element of the second row.
Good luck!
Code: Select all
function A = readtri ()
fid=fopen('triangle.txt');
A=zeros(100,100);
for n=1:100
A(n,1:n)=fscanf(fid,'%d',n);
end
endfunction
Good luck!
-
- Posts: 3
- Joined: Mon Apr 04, 2011 1:47 pm
Re: Entering Array in MATLAB (eg. Prob18)
Wrongrook...
Thank you... the problem became trivial once the code was in (definitely the shortest time I've solved a PE problem... not counting the time it took me to do P18 though)... Now I need to just go back and really understand what those bits of code are doing...
I'm not a big fan of the problems which require you to read in data!
Thank you again.
Thank you... the problem became trivial once the code was in (definitely the shortest time I've solved a PE problem... not counting the time it took me to do P18 though)... Now I need to just go back and really understand what those bits of code are doing...
I'm not a big fan of the problems which require you to read in data!
Thank you again.
- Lord_Farin
- Posts: 239
- Joined: Wed Jul 01, 2009 10:43 am
- Location: Netherlands
Re: Entering Array in MATLAB (eg. Prob18)
You can rejoice in the fact that most later problems, when in need of data, require you to also generate the data yourself.owen.daniel wrote:I'm not a big fan of the problems which require you to read in data!
Paradoxically enough, that can simplify things considerably
-
- Posts: 16
- Joined: Sun Mar 02, 2014 2:32 pm
Re: Entering Array in MATLAB (eg. Prob18)
If you know C or C++, you should be able to write any function you like in C and call it from MATLAB.