Add/change values in matrix of custom axis values

1 次查看(过去 30 天)
Hi all.
There are two things I struggle with. One is to create a matrix with custom axis values. The second one is to use those custom values as coordinates so that I can change the values if required.
For example:
y\x 0.2 | 0.22 | 0.24 | 0.26
0.2 | 0 | 0 | 0 | 0
0.22| 0 | 1 | 0 | 2
And now, If I want to change a value I can do Z(0.26,0.22) = 3
I would appreciate any helps with this. Thank you.

回答(1 个)

Guillaume
Guillaume 2014-11-25
编辑:Guillaume 2014-11-25
The syntax you're asking does not exist in matlab. There's no concept of coordinates for the rows and columns of a matrix, it's just the nth column and mth row, where m and n are strictly positive integers.
The simplest way to do what you want would be with:
xcol = [0.2 0.22 0.24 0.26];
yrow = [0.2; 0.22];
z = [0 0 0 0
0 1 0 2];
%these three asserts are optional. They make sure everything is as it should
assert(all(diff(xcol)>0)) %coordinates must be strictly monotonically increasing
assert(all(diff(yrow)>0)) %coordinates must be strictly monotonically increasing
assert(size(z, 1) == numel(yrow) && size(z, 2) == numel(xcol)) %size of z == size of yrow*xcol
coordtoindex = @(x,y) sub2ind(size(z), find(yrow == y), find(xcol == x));
%note that if xcol, yrow or size(z) changes you need to redeclare coordtoindex
z(coordtoindex(0.26, 0.22)) = 3
Another option would be to create a class for which you overload subsref. If you're not familiar with writing classes in matlab, and OOP in general, forget about it.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by