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 件のコメント
David Hill
2021 年 6 月 7 日
nada=zeros(64);
for a=1:64
for b=1:64
nada(a,b)=delta(a,b);
end
end
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!