How to make 2-dimensional array in matlab?

I want to make a 2D dij array (i and j are indices). I want to be able to do dij = di, j-1 + (di, j-1-di-1, dj-1) / (4 ^ j-1). My idea for this is to make arrays in 1D and then combine them into a 2D array. Is there an easier way to do this?

+4
source share
2 answers

Since n is 10, I would definitely just allocate the array as follows:

d = zeros(n,n) 

Then type d (1,1) into your element and process the first line explicitly (I assume that you just don't include the terms related to the previous line) before iterating through the rest of the lines.

+6
source

Keep in mind that Matlab starts numbering with 1. Then useful features are

 zeros(m,n) % Makes a 2D array with m rows and n columns, filled with zero ones(m,n) % Same thing with one reshape(a , m , n) % Turns an array with m*n elements into am,n square 

The latter is useful if you build a linear array, but then want to make a square one out of it. (If you want to count columns instead of rows, reshape(a,n,m)' .

You can also perform an external product of two vectors:

 > [1;2;3]*[1 2 3] ans = 1 2 3 2 4 6 3 6 9 

In order to actually build an array with the math you are describing, you will probably have to iterate over it on at least one axis with the for loop.

+2
source

Source: https://habr.com/ru/post/1303887/


All Articles