Unable to assign values to some matrix-cells
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, I'm currently trying to assign values in a matrix defined in a class. My problem is that the last row and last column values stay unchanged... When 'debugging' I can see that they are well traveled in the for-loop.
Thank you in advance for any suggestion.
Here is a screenshot of my matrix:
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
And the different pieces of code that can be useful
E = Ez(1, 5);
for x=-2:2
for y=-2:2
E.set(x,y,1,0);
end;
end;
class Ez:
classdef Ez < handle
properties
total_timestamps
matrices
end;
methods
function obj = Ez(total_timestamps, dimensions)
obj.total_timestamps = total_timestamps;
for i=1:total_timestamps
obj.matrices{i} = field(dimensions+(total_timestamps-i)*2);
end;
end;
function obj = set(obj, x,y,z,t)
if(t >= 0)
obj.matrices{t+1}.set(x,y,z);
end;
end;
end;
end
class field:
classdef field < handle
properties
width;
height;
values;
end;
methods
function obj = field(varargin)
if(nargin == 1)
obj.width = varargin{1};
obj.height = varargin{1};
obj.values = zeros(varargin{1});
else
obj.width = varargin{1};
obj.height = varargin{2};
obj.values = zeros(varargin{2},varargin{1});
end;
end;
function obj = set(obj, x,y,z)
actual_x = obj.toActualX(x);
actual_y = obj.toActualY(y);
if actual_x < obj.width && actual_x > 0 && actual_y < obj.height && actual_y > 0
obj.values(actual_y,actual_x) = z;
end;
end;
end;
methods(Access = private)
function actual_x = toActualX(obj,x)
if(mod(obj.width,2) ~= 0)
actual_x = (obj.width+1)/2 + x;
else
actual_x = obj.width/2 +1 + x;
end;
end
function actual_y = toActualY(obj,y)
if(mod(obj.height,2) ~= 0)
actual_y = (obj.height+1)/2 + y;
else
actual_y = obj.height/2 +1 + y;
end;
end
end;
end
0 件のコメント
採用された回答
Image Analyst
2013 年 11 月 23 日
Try <= :
if actual_x <= obj.width && actual_x > 0 && actual_y <= obj.height && actual_y > 0
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Argument Definitions についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!