Matrix Value Replacement Problem
古いコメントを表示
I'm intending on altering the values of a matrix (A) based on values in a another (B) in order to completely fill out the original matrix. Here's what I mean:
A= 0 0 0 1 0
0 1 0 1 0
1 0 0 0 1
0 0 0 0 1
1 0 0 1 1
B= 0 0 0 2 0
0 2 0 3 0
4 0 0 0 1
0 0 0 0 4
2 0 0 3 4
based on the value that appears at the corresponding location in matrix B determines which other location a point in A will populate. In other words if B(1,4)=2 then the A(1,4)=1 and A(1,5)=1. However this requires that A(1,5)=0 before being replaced with a 1.
if B(x,y)==1 && A(x,y-1)==0
A(x,y)=1
A(x,y-1)=1
elseif B(x,y)==2 && A(x,y+1)==0
A(x,y)=1
A(x,y+1)=1
elseif B(x,y)==3 && A(x+1,y)==0
A(x,y)=1
A(x+1,y)=1
elseif B(x,y)==4 && A(x-1,y)==0
A(x,y)=1
A(x-1,y)=1
end
this is the code I currently have however whenever I run it it only replaces values in the y+1 direction and none of the others. Am I writing this correctly? Is their a better way to write this? Any help would be greatly appreciated thank you!
10 件のコメント
Guillaume
2019 年 1 月 10 日
Presumably, these tests are inside one or more loops. Possibly, the problem is with the loops. Certainly, we'd need to know what the (badly named) x and y are.
In your above example, what is the result that should be produced? Most likely, no loop is needed.
William Diaz
2019 年 1 月 10 日
Bob Thompson
2019 年 1 月 10 日
Does your x and y selection take place within a loop? Because if not then you're only looking at one set of values for x and y, so you can only make one set of changes.
William Diaz
2019 年 1 月 10 日
So, you expressely want to do the replacement one element at a time at a random location, not remplace all valid positions all in one go?
It doesn't look like there's anything wrong with your tests, so if the code doesn't work it's with something else that you're not showing, so give us the whole code.
Note that x usually denotes the horizontal direction, the columns, not the rows. I would recommend you rename your variables row, column to avoid ambiguity (or r, c at a push)
William Diaz
2019 年 1 月 10 日
Guillaume
2019 年 1 月 10 日
I'll have a look later on in details at your code. In the meantime, I would strongly recommend that you change the way you code and use functions instead of scripts called from scripts as your current structure is going to make it very difficult to debug and maintain very quickly.
E.g. Collapse should be written as:
function M = collapse(M, D, x, y)
W=1; S=2; N=3; E=4; %or make D a categorical array
if D(x,y)==E && M(x,y+1)==0
M(x,y)=2;
M(x,y+1)=2;
elseif D(x,y)==W && M(x,y-1)==0
M(x,y)=2;
M(x,y-1)=2;
elseif D(x,y)==N && M(x+1,y)==0
M(x,y)=2;
M(x+1,y)=2;
elseif D(x,y)==S && M(x-1,y)==0
M(x,y)=2;
M(x-1,y)=2;
end
end
and called appropriately
William Diaz
2019 年 1 月 17 日
Image Analyst
2019 年 1 月 17 日
Why are you choosing the first index as x, and the second one as y???
You should know that arrays are indexed as (y, x), NOT (x, y).
See if that fixes your problem.
William Diaz
2019 年 1 月 18 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!