How to define cells' position in array?

Hi guys!
I have 2D square array, I can define cells in top corners and bottom corners, but I am struggling to find a formula, which I could use to define rest of cells in the square array of any size. Any help will be highly appreciated!
Many thanks in advance!
M=[1,2,3,4; 5,6,7,8; 9,10,11,12; 13,14,15,16]
a =size(M,1)%number of rows
b =size(M,2)%number of columns
%top left left cell will be defined as
M(1,1)
%top right cell will be defined as
M(1,b)
%bottom left corner
M(b,1)
%bottom right corner
M(end)

Respuestas (1)

Stephen23
Stephen23 el 26 de Nov. de 2018
Editada: Stephen23 el 26 de Nov. de 2018

0 votos

M(1,1) % TL
M(1,end) % TR
M(end,1) % BL
M(end,end) % BR

6 comentarios

Yuri Zhukov
Yuri Zhukov el 26 de Nov. de 2018
thanks, but what about the rest of cells? My question was how to define the cells between them.
Stephen23
Stephen23 el 26 de Nov. de 2018
Editada: Stephen23 el 26 de Nov. de 2018
"thanks, but what about the rest of cells? My question was how to define the cells between them."
I checked your question very carefully, and there is no mention of "between", "intermediate", "range", "column", "row", or any words that indicate that you wanted to know about the elements in between the corners. You wrote that you are "struggling to find a formula, which I could use to define rest of cells...", but you do not define what the "rest of cells" are: are these all elements of the matrix? Or only in specific columns or rows?
Because you did not specify about what "rest" means, I have no idea if you want the first/last columns, or first/last rows, or all elements of the entire matrix, or something else entirely. You did not specify this anywhere, which forces us to guess what you want.
I will guess that you want the first/last rows and first/last columns:
M(1:end,1) % first column
M(1:end,end) % last column
M(1,1:end) % first row
M(end,1:end) % last row
Or perhaps like this:
M(1:end,[1,end]) % first and last columns
M([1,end],1:end) % first and last rows
If that is not what you want, then please actually describe (in words or examples) what you expect to get.
Yuri Zhukov
Yuri Zhukov el 26 de Nov. de 2018
Editada: Yuri Zhukov el 26 de Nov. de 2018
sorry for poor description. And I very appreciate your efforts to understand my request. I am asking about cells, not columns. I am trying to be more specific, there are 16 cells in my array, I already know formula for top and bottom corners. So, is it possible to define rest of 12 cells in the same way? If it would be 3x3 array it would be something like
M(1,1) M(1,b-1) M(1,b) and etc. But I am trying to define it in the way to use it for any size square array. Hope it's clarifying my question.
Many thanks!
Stephen23
Stephen23 el 26 de Nov. de 2018
Editada: Stephen23 el 26 de Nov. de 2018
"So, is it possible to define rest of 12 cells in the same way?"
Not really. You could certainly define them individually, like this:
M(1,1)
M(1,2)
M(1,3)
...
But this would be a very bad approach to writing MATLAB code, and I would not reccomend doing this. Repeating code like that is a sign that you should be using a loop, or vectorizing the code.
Another solution would be to split the numeric array up into a cell array: this can be useful in some situations, but so far you have told us nothing to indicate that this would be appropriate for your use case.
"But I am trying to define it in the way to use it for any size square array"
The reason why you are struggling is very simple: because that is not how MATLAB works. Instead of using the features of MATLAB (e.g. operations on complete matrices and arrays) you are trying to fight it by doing something quite inefficient and complex.
The simple MATLAB way to do what you asked for is to use the vectors and indexing that I have already showed you. You also need to learn to write vectorized code:
and avoid splitting up your data. In MATLAB data is much easier to process when it is kept together as much as possible.
If you actually explain what your goal/task is, then we can help find a better/simpler/more efficient way to achieve it.
Yuri Zhukov
Yuri Zhukov el 26 de Nov. de 2018
next calculation depends on cell's position, that's why I am trying to define their postions. For arrays 2x2 or 3x3 I can hardcode next calculations, but it is not how it supposed to work. In total it's 4 scenarios of calculation for arrays 2x2 and 9 scenarios of calculation for arrays 3x3 and bigger.
And it's only for square arrays, that's why I was thinking, that knowing that number of columns equals to number of rows would be possible to find the rule/formula how to define it.
Stephen23
Stephen23 el 26 de Nov. de 2018
"next calculation depends on cell's position, that's why I am trying to define their postions"
Getting indices is easy:
S = size(M)
rows = 1:S(1)
cols = 1:S(2)
"For arrays 2x2 or 3x3 I can hardcode next calculations, but it is not how it supposed to work. In total it's 4 scenarios of calculation for arrays 2x2 and 9 scenarios of calculation for arrays 3x3 and bigger."
I have no idea what that means. Please show an example.

Iniciar sesión para comentar.

Productos

Versión

R2018b

Preguntada:

el 26 de Nov. de 2018

Comentada:

el 26 de Nov. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by