How do I add values of a function to a matrix?

4 visualizaciones (últimos 30 días)
Goncalo Costa
Goncalo Costa el 7 de Jun. de 2021
Comentada: Goncalo Costa el 7 de Jun. de 2021
I have an empty square matrix, and intend to put the different outputs of the function onto each indivudal matrix index space.
How do I create a code that will move across the matrix, horizontally via every matrix point? Will this require a for loop?
The code for the delta function and for the empty square matrix is presented below:
function [d] = delta(a, b) %delta function showing a value of 1 at the centre (32,32)
d=0;
if a==32 && b ==32
d=1;
end
nada = zeros(64) %square matrix with zeros values
This should result in a square matrix full of zeros but with a 1 at the centre point (32,32) .
I intend to do something similar to what was asked for in: https://uk.mathworks.com/matlabcentral/answers/140889-store-all-iteration-loop-outputs-in-a-matrix . But to keep the values in a square matrix and I don't know how to indicate the programme to change rows and keep moving through the matrix.
  2 comentarios
Stephen23
Stephen23 el 7 de Jun. de 2021
Note that (32,32) is not the "centre point" of a 64x64 matrix: there is not single "centre point" if there are an even number of rows or columns. There is a single "centre point" if there are an odd number of rows and columns.
"How do I create a code that will move across the matrix, horizontally via every matrix point?"
Whatever way you use (loop, logical indexing, etc) you will probably need to use indexing of some kind. Before you continue, I strongly recommend that you study the basic ways of indexing in MATLAB:
Goncalo Costa
Goncalo Costa el 7 de Jun. de 2021
I will have a look in those links, thank you so much.

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 7 de Jun. de 2021
N = 64;
idx = reshape(reshape(1:N^2,N,N).', 1, []);
Now idx is the linear indices of the array, in order going across the rows. But now what?
This does not seem to have anything to do with the rest of your question, which is more easily handled like
nada = zeros(64);
nada(end/2,end/2) = 1;

David Hill
David Hill el 7 de Jun. de 2021
nada=zeros(64);
for a=1:64
for b=1:64
nada(a,b)=delta(a,b);
end
end

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by