0

I have a problem, I want to allocate a block of memory of size m and n (so a 2D matrix) where each element of the matrix is a struct of 2 variables ( a point with 2 double coordinates x and y). The language I want to write this in is MATLAB I have tried something like this:

left = zeros(height, width);
for i = 1 : width
  for j = 1 : height
     var.x = someValue;
     var.y = someOtherValue;
     left(i,j) = var;

The piece of code from above doesn't work.. How can I fix the problem?

Suever
  • 64,497
  • 14
  • 82
  • 101
Mircea Paul Muresan
  • 628
  • 1
  • 9
  • 23

1 Answers1

1
left(height,width) = struct();
for i = 1 : width
  for j = 1 : height
    left(j,i).x = someValue;
    left(j,i).y = someOtherValue;
  end
end

should do what you want

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
Gianni
  • 458
  • 5
  • 17
  • Thaks for the response, however it doesn't work... the error is: Subscripted assignment between dissimilar structures.Try implementing it for 1 value to see – Mircea Paul Muresan Nov 10 '14 at 15:33
  • 1
    I do not have matlab here, it seems to work in Octave. Did you `clear left` before trying? – Gianni Nov 10 '14 at 16:22
  • Thank you for the response! I was not clearing left before the doing the operations. I put a clear left before the instructions and everything works ok :). – Mircea Paul Muresan Nov 12 '14 at 07:47