How do I add values of a function to a matrix?
4 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 7 Jun 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:
Answers (2)
Walter Roberson
on 7 Jun 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;
0 Comments
David Hill
on 7 Jun 2021
nada=zeros(64);
for a=1:64
for b=1:64
nada(a,b)=delta(a,b);
end
end
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!