Borrar filtros
Borrar filtros

Add/change values in matrix of custom axis values

1 visualización (últimos 30 días)
Kornelia
Kornelia el 25 de Nov. de 2014
Editada: Guillaume el 25 de Nov. de 2014
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.

Respuestas (1)

Guillaume
Guillaume el 25 de Nov. de 2014
Editada: Guillaume el 25 de Nov. de 2014
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.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by