function with 4x4 combinations

2 views (last 30 days)
MadjeKoe
MadjeKoe on 26 Oct 2020
Commented: Rik on 26 Oct 2020
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
MadjeKoe
MadjeKoe on 26 Oct 2020
Yeah, my teacher wants me to put everything into a function. I personally also think that copy-pasted code works fine..
Rik
Rik on 26 Oct 2020
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)

Categories

Find more on Argument Definitions in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!