Function is not working

Hi all! I have to make the following function work. Can someone please help me??? I've been sitting on it all day long and I know that there a mistakes in it, but I do not know how to fix them.
ori4 = res(:,3);
ori3 = res(:,4);
ori2 = res(:,5);
ori1 = res(:,6);
targets = res(:,7);
responses = res(:,8);
resp1 = responses(targets==1);
resp2 = responses(targets==2);
resp3 = responses(targets==3);
resp4 = responses(targets==4);
function [err] = rerange(responses, ori, targets, target)
resp = responses(targets==target);
ori_new = ori(targets==target);
err = resp(targets==target) - ori_new(targets==target);
err(err<-90) = err(err<-90)+180;
err(err>90) = err(err>90)-180;
end

5 个评论

Image Analyst
Image Analyst 2020-10-13
编辑:Image Analyst 2020-10-13
You need to assign res. It will throw an error without it.
Plus your main script never even calls your rerange() function at all!
Can you maybe help me fix it? I still cannot get it to work..
Stephen23
Stephen23 2020-10-14
编辑:Stephen23 2020-10-14
@Maddy Koenraad: please upload some sample data in a .mat file by clicking the paperclip button.
Note that using numbered variables like that will make your code more complex and/or slow and inefficient. You would be much better off just using indexing rather than numbering variables.
@Maddy Koenraad: what value/s does target have?

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2020-10-14
编辑:Stephen23 2020-10-14
The basic problem is that you keep repeating the same indexing, e.g.:
resp = responses(targets==target);
..
err = resp(targets==target)
but note that resp is already a sub-vector of responses, which you then try to use exactly the same index again. If resp is a smaller subvector then it is quite likely that the second time you use that index will throw an error (as it does with your example data). The solution is to only index once , after which you can just keep using the subvector for anything.
I also replaced those anti-pattern numbered variables with basic indexing. Do NOT number variables like that.
This runs without error (only you can check if it does what you want):
S = load('simulated_serial_bias_dataset.mat');
res = S.res;
targets = res(:,7);
responses = res(:,8);
orx = [3,4,5,6];
tgt = [1,2,3,4];
num = numel(orx);
out = cell(1,num);
for k = 1:num
idx = targets==tgt(k);
resp = responses(idx);
ori = res(idx,orx(k));
err = resp - ori;
err(err<-90) = err(err<-90)+180;
err(err>90) = err(err>90)-180;
out{k} = err;
end
out = [out{:}]; % optional

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

标签

提问:

2020-10-13

编辑:

2020-10-14

Community Treasure Hunt

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

Start Hunting!

Translated by