Struct contents reference from a non-struct array object

hello
I am new to Matlab and I am trying to minimize the following function with fmincon:
function [ CRRA ] = CRRA(x)
tur = load('tur.mat');
wei = load('wei.mat');
ret = tur.tur *x';
first = 1 + ret;
util = ((first.^(-85))/(-85));
wut = wei. * util;
CRRA = -mean(wut);
end
tur is a 140x500 matrix, x is what I am looking for, i.e. a 1x500 matrix, and wei is a 140x1 matrix. The problem is the line
wut = wei. * util
where I want to multiply wei with util element by element. I get the message:
Struct contents reference from a non-struct array object
with reference to this line. I also tried wut = times (wei, util) but obviously didn't work.
When I copy wut = wei. * util; and paste it in the command editor it calculates wut correctly. I would really appreciate any help.
Thank you

 채택된 답변

Steven Lord
Steven Lord 2020년 10월 12일
편집: Steven Lord 2020년 10월 12일
function [ CRRA ] = CRRA(x)
tur = load('tur.mat');
wei = load('wei.mat');
The preceding two lines load the MAT-file each and every time this function is called. Instead load the data once before calling fmincon and pass them into your objective function as additional parameters.
When you do this you can pass in tur.tur and wei.wei as the parameters, eliminating the need to extract those fields from each struct array each time the function is executed. You successfully extracted the field from tur on this line:
ret = tur.tur *x';
first = 1 + ret;
util = ((first.^(-85))/(-85));
but you forgot to extract the field on this line (in which I've replaced ". *" with ".*" to correct the original error) which led to the new error.
wut = wei.* util;
CRRA = -mean(wut);
end

추가 답변 (1개)

Matt J
Matt J 2020년 10월 12일

0 개 추천

I think you just have to get rid of the extra whitespace between . and *

댓글 수: 3

I just tried that. I got the message
Undefined operator '.*' for input arguments of type 'struct'.
Matt J
Matt J 2020년 10월 12일
편집: Matt J 2020년 10월 12일
Use
>> dbstop if error
to pause execution where the error occurs and see what types of variables are being multiplied.
I get the following message:
Error: File: CRRA.m Line: 8 Column: 24
Unexpected MATLAB operator.
Error in fmincon (line 536)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in globalsearchnlp
Error in GlobalSearch/run (line 340)
globalsearchnlp(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,options,localOptions);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
Failure in initial call to fmincon with user-supplied problem structure.
Line 8 is: wut = wei. *util;
If i delete this line and in the code I posted originally change wut to util in the final line, everything works fine.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

태그

질문:

2020년 10월 12일

댓글:

2020년 10월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by