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.
0 件のコメント
回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!