How do I add values of a function to a matrix?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2021-6-7
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:
回答(2 个)
Walter Roberson
2021-6-7
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!