function with 4x4 combinations

Hi guys, i'm a beginner with matlab and i'm trying to put the below into a function. I need all combinations to be calculated. So when the err = 1, all other orientations must be subtracted from ori1. And when err = 2, all other orientations must be subtracted from ori2. Can somebody please help me with this?? I've tried a lot of different things but I cannot get it to work..
ori1 = res(:,3);
ori2 = res(:,4);
ori3 = res(:,5);
ori4 = res(:,6);
err1 = out(:,1);
err2 = out(:,2);
err3 = out(:,3);
err4 = out(:,4);
relativeorientation = ori1 - ori2
responseerror = err1
sortrows(relativeorientation,responseerror)
out = sortrows([relativeorientation responseerror]);
plot(out)

5 Comments

I don't understand what you are describing. A simple example of inputs and outputs would be helpful.
MadjeKoe
MadjeKoe on 26 Oct 2020
Edited: Rik on 26 Oct 2020
Im sorry, my question is indeed a bit unclear. When the 2nd stimuli is the target, I want to calculate the relative orientations of the other stimuli compared to the target. Then I want to plot the relative orientations against the error response, to see if the response to the target is influenced by the orientations of the other targets. Below is an example of where I calculated the relative orientation of stimuli 3 to the orientation of the target, stimuli 2. Then I sorted the rows in the order of the lowest to the highest relative orientation. I want to make a function that automatically calculates the the relative orientations of all stimuli, compared to the target, sorted from low to high. And if possible to also automatically make the different plots.
rel23 = ori2(targets==2) - ori3(targets==2);
out23 = sortrows([rel23 err2]);
plot(mov23)
It sounds like you need a loop.
If you write out everything manually, what would it look like? For this example numbered variable and copy-pasted code are fine, but normally you shouldn't be using them.
Yeah, my teacher wants me to put everything into a function. I personally also think that copy-pasted code works fine..
No, I mean you can show us what you want by copy-pasting repeated code. That way we can clearly see what should happen. That way you don't need to write a complicated block of text. We will also be able to verify that the output is the exact same.
%example of copy-paste code:
rng(0);x=rand(1,5);y=rand(1,5); %set rng so the result of the three methods can be compared
x1=x(1);y1=y(1);z1=x1+y1;
x2=x(2);y2=y(2);z2=x2+y2;
x3=x(3);y3=y(3);z3=x3+y3;
x4=x(4);y4=y(4);z4=x4+y4;
x5=x(5);y5=y(5);z5=x5+y5;
z=[z1 z2 z3 z4 z5];
z_method_1=z;
Here we can clearly see what you want, so we can suggest how to implement something better:
rng(0);x=rand(1,5);y=rand(1,5);
z=zeros(size(x));
for n=1:numel(x)
z(n)=x(n)+y(n);
end
z_method_2=z;
Or better:
rng(0);x=rand(1,5);y=rand(1,5);
z=x+y;
z_method_3=z;
By setting the random seed every time, we can be sure that the input (and therefore the output) is the same every time.
clc % both statements below should return true
isequal(z_method_1,z_method_2)
isequal(z_method_1,z_method_3)

Sign in to comment.

Answers (0)

Tags

Asked:

on 26 Oct 2020

Commented:

Rik
on 26 Oct 2020

Community Treasure Hunt

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

Start Hunting!