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!