matrix with general rule
4 ビュー (過去 30 日間)
古いコメントを表示
Dear all, I have the code below:
m=6;
M=21;
Z_matrix=zeros(M+2,M+2);
A=1;
B=2;
for i=1:1:m
for j=1:1:i
k=(i-1)*i/2+j;
Z_matrix(k+1,func(i-1,j-1))=B;
Z_matrix(k+1,func(i-1,j))=A;
Z_matrix(k+1,func(i-1,j+1))=B;
Z_matrix(k+1,func(i,j-1))=A;
Z_matrix(k+1,func(i,j))=B;
Z_matrix(k+1,func(i,j+1))=A;
Z_matrix(k+1,func(i+1,j-1))=B;
Z_matrix(k+1,func(i+1,j))=A;
Z_matrix(k+1,func(i+1,j+1))=B;
end
end
where
function z=func(x,y)
z=(x-1)*x/2+y;
end
However, my problem is the function z=func(x,y) can give the index which might be larger than M+2 or less than 1. I mean if the index is larger or smaller than the actual, we just simply cancel this term. This is something like the boundary effect, I guess
Do you have any suggestion for this problem? Thanks
0 件のコメント
回答 (2 件)
John D'Errico
2016 年 5 月 3 日
Suggestion for what? It is impossible to create the zeroth column of a matrix in MATLAB. Indices cannot be negative. If the index is larger than the matrix, then MATLAB dynamically will resize the matrix, adding columns filled with zeros.
So your question has no meaning in terms of MATLAB syntax. You need to decide what to do when that index is less than zero.
POSSIBLY...
I note that you use effectively the same rule for the rows. There you compute k using the same rule, but then you add 1. Possibly, you wanted to do exactly that. But how can I know? It is your matrix, your rules.
2 件のコメント
John D'Errico
2016 年 5 月 3 日
編集済み: John D'Errico
2016 年 5 月 3 日
So have a second function that IF the argument is less than 1, it returns an empty array. Otherwise, it should return the argument.
function ind = checkneg(ind)
% returns ind only if ind >= 1. otherwise, empty
if ind < 1
ind = [];
end
So, now your code will look like
Z_matrix(k+1,checkneg(func(i-1,j-1)))=B;
If the column index is an empty array, then it effectively skips executing that line.
The row index will never have that problem, so no need to worry there.
Star Strider
2016 年 5 月 3 日
Change your for limits to:
for i=2:1:m-1
for j=2:1:i.
That way, you will not overwrite your matrices. You may need to experiment, and recalculate ‘k’ to accommodate these changes.
Another option is to keep the for index limits as they are, and to limit the indices to the minimum and maximum values for the matrix (as I usually do).
For example:
Z_matrix(k+1,func(max(1,i-1),max(1,j-1)))=B;
...
Z_matrix(k+1,func(min(M+2,i+1),min(M+2,j+1)))=B;
This simply avoids overwriting the matrix limits. You have to decide if this is appropriate to what you want to do.
2 件のコメント
Star Strider
2016 年 5 月 3 日
編集済み: Star Strider
2016 年 5 月 3 日
That’s one option. It depends on what you want your code to do. The only problem I see with the if block is that it could create discontinuities in your result.
You have already created your matrix as being (M+2,M+2), so if you use the max and min limit approach, and then trim the first and last rows and columns, you would get your (M,M) matrix. If you want it to be (M+2,M+2), consider creating it as (M+4,M+4), and doing the same thing with the max and min limits, then remove the first and last rows and columns. That would create your full matrix, without having to make any compromises in constructing it.
I would also look through the conv2 function to see if it does what you want.
I ran your code, but since I don’t understand the result you want, it’s difficult for me to suggest a specific approach.
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!