필터 지우기
필터 지우기

In place function on a structure or a class

조회 수: 3 (최근 30일)
Or Nahir
Or Nahir 2018년 11월 12일
편집: Or Nahir 2018년 11월 12일
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

답변 (1개)

Or Nahir
Or Nahir 2018년 11월 12일
편집: Or Nahir 2018년 11월 12일
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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by