Entering Array in MATLAB (eg. Prob18)

In this forum members can discuss topics about specific programming languages.
Post Reply
owen.daniel
Posts: 3
Joined: Mon Apr 04, 2011 1:47 pm

Entering Array in MATLAB (eg. Prob18)

Post by owen.daniel »

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.
wrongrook
Posts: 662
Joined: Sat Oct 17, 2009 10:39 pm

Re: Entering Array in MATLAB (eg. Prob18)

Post by wrongrook »

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]};
wrongrook
Posts: 662
Joined: Sat Oct 17, 2009 10:39 pm

Re: Entering Array in MATLAB (eg. Prob18)

Post by wrongrook »

To use cell arrays you might want to try a function such as:

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
You can then use this by doing things like:

Code: Select all

A=readtri();
disp(A{2}(1))
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)
wrongrook
Posts: 662
Joined: Sat Oct 17, 2009 10:39 pm

Re: Entering Array in MATLAB (eg. Prob18)

Post by wrongrook »

To avoid cell arrays and just get Matlab to enter the zeros for you, you could use a function like this:

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
You can then use normal Matlab A(2,1) to access the first element of the second row.

Good luck!
owen.daniel
Posts: 3
Joined: Mon Apr 04, 2011 1:47 pm

Re: Entering Array in MATLAB (eg. Prob18)

Post by owen.daniel »

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.
User avatar
Lord_Farin
Posts: 239
Joined: Wed Jul 01, 2009 10:43 am
Location: Netherlands

Re: Entering Array in MATLAB (eg. Prob18)

Post by Lord_Farin »

owen.daniel wrote:I'm not a big fan of the problems which require you to read in data!
You can rejoice in the fact that most later problems, when in need of data, require you to also generate the data yourself.
Paradoxically enough, that can simplify things considerably :)
Image
brainiac1530
Posts: 16
Joined: Sun Mar 02, 2014 2:32 pm

Re: Entering Array in MATLAB (eg. Prob18)

Post by brainiac1530 »

If you know C or C++, you should be able to write any function you like in C and call it from MATLAB.
Post Reply