Add/change values in matrix of custom axis values

조회 수: 2 (최근 30일)
Kornelia
Kornelia 2014년 11월 25일
편집: Guillaume 2014년 11월 25일
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에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by