Borrar filtros
Borrar filtros

In place function on a structure or a class

3 visualizaciones (últimos 30 días)
Or Nahir
Or Nahir el 12 de Nov. de 2018
Editada: Or Nahir el 12 de Nov. de 2018
Hi,
I've encountered the following problem with in place editing. I have an in-place function, x = f(x). If I put x in a structure it is no longer in-place. Any ideas?
In-place:
function tmp
format debug
x = rand(1, 5);
x
x = cumsum(x);
x
end
/
>> tmp
x =
Structure address = 13e0854b0
m = 1
n = 5
pr = 60000f633020
0.1068 0.6538 0.4942 0.7791 0.7150
x =
Structure address = 13e11e5c0
m = 1
n = 5
pr = 60000f633020
0.1068 0.7605 1.2547 2.0337 2.7488
Not in-place:
function tmp
format debug
s.x = rand(1, 5);
s.x
s.x = cumsum(s.x);
s.x
end
/
>> tmp
ans =
Structure address = 13e11eda0
m = 1
n = 5
pr = 60000f633020
0.6099 0.6177 0.8594 0.8055 0.5767
ans =
Structure address = 13e11da60
m = 1
n = 5
pr = 60000b1ca5e0
0.6099 1.2275 2.0870 2.8925 3.4692

Respuestas (1)

Or Nahir
Or Nahir el 12 de Nov. de 2018
Editada: Or Nahir el 12 de Nov. de 2018
I think this behavior is even more interesting. It seems as if a struct cannot free the pointer to an array. So once you assigned it a value, it would most likely be copied in the future.
In this example no copy is made:
function tmp
format debug
x= rand(1,5);
x
y = x;
y
clear y
x = x+1;
x
end
In this example x is copied in the line x = x+1, probably since clear s, didn't free the array.
function tmp
format debug
s.x = rand(1, 10);
x = s.x;
clear s
x
x = x+1;
x
end

Categorías

Más información sobre Structures 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