1

I have this problem in Mathematica :

  L=16;
  f[x_]:=-x;
  mlat = Table[2 RandomInteger[] - 1, {L}, {L}];
  ArrayPlot[mlat, ColorFunction -> (If[# == 1, White, Black] &), Mesh -> All]

and I did this in Matlab:

 L=16;
 f=@ (x) -x;
 mlat=2*randint(L,L)-1;
    if mlat(:,:)==1   
      plot(mlat,'ws')
      hold on
    else
        plot(mlat,'ks')
        hold off
        grid on
    end

but I can't get the graph.

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
George
  • 5,808
  • 15
  • 83
  • 160

1 Answers1

2

First, you want to create an array with only ones and zeros, which you do using randi

L = 16;
mlat = 2*(randi([0,1],L,L)-0.5);

Then, you can display this as an image (I like to open an new figure for every plot)

figure
imshow(mlat,[]) %# [] scales to min...max

To make the image bigger, set axes size to 90% of the figure window

set(gca,'Units','normalized','Position',[0.05 0.05 0.9 0.9],'visible','on')

enter image description here

Note that the axes label correspond to the index of matrix elements, so (1,1) is top left.

Jonas
  • 74,690
  • 10
  • 137
  • 177
  • Hello,i am looking at it now,i edited my post.I have a function f. – George Feb 18 '11 at 13:48
  • @George: And this function `f` does what? – Jonas Feb 18 '11 at 13:50
  • It is the spin reverse operator.I don't know how to implement it in my loop. – George Feb 18 '11 at 13:55
  • And the mlat has only values -1 and 1,not 0 and 1 as you said.I think i have this right. – George Feb 18 '11 at 13:58
  • @George: Oh, I missed the thing with -1 and 1. I'm editing this right away. – Jonas Feb 18 '11 at 14:00
  • Also,Jonas whenever you have time ,please see my post http://stackoverflow.com/questions/5032029/map-command-and-few-other-from-mathematica-to-matlab if you can help..Thanks... – George Feb 18 '11 at 14:31
  • @George: What does `f` do? Invert all spins, invert only some spins? – Jonas Feb 18 '11 at 14:38
  • From the mlat table the -1 represents the spins with downwards direction and the 1 the spins with upwards.With the plot we have the +1 spins with white color and the -1 with black.In the command " ArrayPlot[mlat, ColorFunction -> (If[# == 1, White, Black] &), Mesh -> All]" the "&" symbol means implementing the function.I can't do this in matlab.So,if i am not mistaking f goes to all the mlat table. – George Feb 18 '11 at 14:43
  • @George: If `f` inverts all spins, then you just use `-mlat` and you're done. Or am I missing something here? – Jonas Feb 18 '11 at 14:47