Create a square array in matlab

In summary: I would recommend colormap(jet).shading interp makes the color between the data points smooth.This is the best quality plot you will get, unless you want to start artificially creating your own data points - which is not recommended.J77.In summary, the conversation discusses the creation of a square array and plotting a vector of data over it. The participants suggest different methods such as using surf or bar3, but ultimately the data provided is not enough to create a 4x4 mesh. The conversation ends with a suggestion to use shading interp for a smooth surface and the use of different colormaps for better visualization.
  • #1
tommyers
58
0
Hi,

I realize this is a physics forum, but thought someone may be able to help!

I would like to create a square array i.e 4x4 and then plot a vector of data (16 elements) over this 4x4 array.

I have created the following code, it doesn't work but that's what I am ideally aiming after:

[X,Y] = meshgrid([1:1:4]);

z = [30 20 20 30 20 10 10 20 20 10 10 20 30 20 20 30];

mesh(X,Y,z)

The reason that I believe that it doesn't work is that my z variable is not a funtion of X or Y. But it isn't in real life so how do I get around this?

Regards

Tom
 
Physics news on Phys.org
  • #2
You need some way for the program to assign an X and Y coordinate to each z value. I guess the easiest way to do this would be to make z a 4x4 matrix. Then you could have something like -

z = z(x,y)

Claude.
 
  • #3
tommyers said:
I would like to create a square array i.e 4x4 and then plot a vector of data (16 elements) over this 4x4 array.

I have created the following code, it doesn't work but that's what I am ideally aiming after:

[X,Y] = meshgrid([1:1:4]);

z = [30 20 20 30 20 10 10 20 20 10 10 20 30 20 20 30];

mesh(X,Y,z)

The reason that I believe that it doesn't work is that my z variable is not a funtion of X or Y. But it isn't in real life so how do I get around this?
You must plot the same sized variables, ie :
size(X)=size(Y)=size(Z)

so you must have z in 4x4 form.

By example, maybe you can (it depends of your problem) just reshape your z :
>> Z = reshape(z,4,4)

Z =

30 20 20 30
20 10 10 20
20 10 10 20
30 20 20 30

then
mesh(X,Y,Z)
and maybe mesh isn't adapted to what you want. Try pcolor instead for a 2D visualisation.
 
  • #4
Specifically, you need to specify the value of z at each point (x,y).

I see you have 16 z values.

Therefore, you may want a 4x4 grid with the z values in a corresponding 4x4 matrix.

My little bit: I always use surf(x,y,z) with "shading interp" for best results.
 
  • #5
Thanks guys - I have now got the following code, so near yet so far:

x = [1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4];
y = [1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4];

x = reshape(x,4,4)
y = reshape(y,4,4)

z = [30 20 20 30 20 10 10 20 20 10 10 20 30 20 20 30];

z = reshape(z,4,4)

z = (z.*x + z.*y);

surf(x,y,z)
colormap hsv
axis([0 4 0 4 0 250])


to generate the attached plot (see below)

The plot is not what I want! I want 16 squares the 4 corners to be the same value, the 4 middle ones to be the same value and the rest a third value - as indicated by the z matrix. I am not bothered about the actual values i.e the 4 corners do not have to be 30 I am more interested in the weighting between each of the 3 values.

Regards

Tom
 

Attachments

  • plot1.jpg
    plot1.jpg
    31.6 KB · Views: 545
  • #6
You are getting a distorted curve because of this line.

z = (z.*x + z.*y);

Take out this line and the code should work. Don't get hung up on making z a function of x and y. It won't work because z has no analytic dependence on x and y, thus you need to manually specify the values of z at each coordinate (which you have already done).

Claude.
 
Last edited:
  • #7
Thanks Claude,

I have now chnaged my code to:

x = [1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4];
y = [1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4];

x = reshape(x,4,4)
y = reshape(y,4,4)

z = [30 20 20 30 20 10 10 20 20 10 10 20 30 20 20 30];

z = reshape(z,4,4)

surf(x,y,z)
colormap hsv
axis([0 4 0 4 0 250])


Although my matrix (coloured part) is still 3 x 3!

I am trying to achieve something like the following:

30 20 20 30
20 10 10 20
20 10 10 20
30 20 20 30

in mesh / surf representation.....

Regards

Tom
 
  • #8
It's 3x3 squares, but 4x4 coordinate points - as you specify.
 
  • #9
Further ... this is what I want.

4
30 20 20 30
3
20 10 10 20
2
20 10 10 20
1
30 20 20 30
0
0 1 2 3 4

where green represent the axis 'ticks' and the purple represent the values between these 'ticks'.

Regards

Tom
 
  • #10
J77,

I NOW realize this - but any suggestions how to change this?

... and keep my matrices the same length?

Tom
 
  • #11
Use bar3 ?

Though you won't get a 'smooth' surface anymore.
 
  • #12
Thats not what I am really looking for!

Tom
 
  • #13
The only other way I see is representing your values at the bisection of two tick marks, ie. at (0.5,0.5)=30, (0.5,1.5)=20, (0.5,2.5)=20, (0.5,3.5)=30 etc.

You don't have enough data to do anything more...

(And there aren't enough points to accurately extrapulate away from the data you have)
 
  • #14
J77,

There is no more data - that's all there is...

I want a 4x4 mesh i.e. 16 squares, with a z term in each!

Have would you do as you suggested in an automated way?

Regards

Tom
 
  • #15
Code:
z = [30 20 20 30; 20 10 10 20; 20 10 10 20; 30 20 20 30];
x=[0.5:1:3.5];
y=[0.5:1:3.5];

surf(x,y,z)
axis([0 4 0 4 10 30])

shading interp
 
  • #16
WOW J77,

That looks amazing!

Although when I turn the shading interp off I still only see 3x3 and the coloring isn't symmetrical - why is this?

The pattern is symmetrical when the shading is one!

Regards

Tom
 
  • #17
Colours in Matlab are quite hard (numerous) to explain... read up on the various colormaps in the helpdesk.
 

1. How do I create a square array in Matlab?

To create a square array in Matlab, you can use the eye function. This function creates a square matrix with ones on the main diagonal and zeros everywhere else. For example, typing eye(3) will create a 3x3 square array.

2. Can I specify the dimensions of the square array?

Yes, you can specify the dimensions of the square array by passing in two arguments to the eye function. The first argument represents the number of rows and the second argument represents the number of columns. For instance, eye(5, 5) will create a 5x5 square array.

3. How do I fill the square array with values other than ones?

You can use the zeros function to create a square array with all zeros. Then, you can use indexing to replace the zeros with any other values you want. For example, array = zeros(4); array(2:3, 2:3) = 5; will create a 4x4 square array with 5s in the center.

4. Can I create a square array with random values?

Yes, you can use the rand or randn functions to create a square array with random values. The rand function generates values from a uniform distribution, while the randn function generates values from a normal distribution. For example, rand(3) will create a 3x3 square array with random values between 0 and 1.

5. How do I visualize a square array in Matlab?

You can use the imshow function to visualize a square array in Matlab. This function displays a grayscale or color image depending on the values in the array. For instance, imshow(array) will display the values in the array as an image.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
570
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top