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

4 visualizzazioni (ultimi 30 giorni)
Goncalo Costa
Goncalo Costa il 7 Giu 2021
Commentato: Goncalo Costa il 7 Giu 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 Commenti
Stephen23
Stephen23 il 7 Giu 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 il 7 Giu 2021
I will have a look in those links, thank you so much.

Accedi per commentare.

Risposte (2)

Walter Roberson
Walter Roberson il 7 Giu 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 il 7 Giu 2021
nada=zeros(64);
for a=1:64
for b=1:64
nada(a,b)=delta(a,b);
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by