I would like to link these two functions together. Where N=3 and Np=4. So i expect 4 groups of 3x3 xy coordinates. I would like to plot each groupcoor vector on the same plot. This is not working.. Is there another way to do this?
function [x,y]=get_number(N)
%Where N is the total number of steps
x(1)=0;
y(1)=0;
%Where STP is the steps.
%The first step is the the coordinates (0,0)
for i=2:N
x(i)= rand()
y(i)= rand()
groupcoor=[x' y']
hold on
end
%pos is the position of the prisoner with every step that they take.
%Every step accounts for one second.
pos= [x', y'];
figure
plot(x',y');
hold on
end
%New function on another script called randomcheck
function [CoordinateGroup]=randomcheck(N,Np)
%[x,y] =get_xy_velocities(N);
hold on
for i=1:Np
Path=get_number(N)
CoordinateGroup(i)=Path
hold on
% x_end=[vector(:,end)]
% y_end=[vector(:,end)]
% x_end(i)=[x(N,end)]
%y_end(i)=[y(N,end)]
end

1 commentaire

Dameon Solestro
Dameon Solestro le 27 Nov 2021
Path only gives an array of the x values, why is that??

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 27 Nov 2021

0 votes

function [x,y]=get_number(N)
get_number returns two 1 x floor(N(1)) outputs (unless N(1) < 1, in which case it returns two 1 x 1 outputs)
Path=get_number(N)
You are only storing one of the outputs, which would correspond to x.
CoordinateGroup(i)=Path
And you are expecting that 1 x N vector to fit within the single output location CoordinateGroup(i)
Perhaps you want to store into a cell array, CoordinateGroup{i} = Path after having constructed Path from the x and y outputs of get_number()
Where N=3 and Np=4. So i expect 4 groups of 3x3 xy coordinates
But your function get_number returns two 1 x N, not N x N or three 1 x N .

11 commentaires

I do create the loop. But i want the end xy values of groupcoor. I was thinking I could do something like
for i=1:Np
x_end(i)=vector(:,end)
y_end(i)=vector(:,end)
hold on
end
but this doesnt work.
Dameon Solestro
Dameon Solestro le 27 Nov 2021
it only loops once and then collects the end 4 times
Walter Roberson
Walter Roberson le 27 Nov 2021
Your current code has no variable named vector and no variables named x_end or y_end so I do not know where you intend that code to go.
Also, vector(:,end) on both lines will be the same value, so x_end(i) and y_end(i) would get assigned the same thing.
If you are expecting vector to be 1 x something then it confuses the readers that you use the : index . If you want the last element of a vector named vector then use vector(end)
sorry i wanted to create an array called x_end for the end points of x
so
i thought
x_end(i)=x(end)
would be useful. but its not. it only uses the first loop's answer
so far i have this
function [x,y]=get_number(N)
%Where N = 3
x(1)=0;
y(1)=0;
%The first step is the the coordinates (0,0)
for i=2:N
x(i)= rand()
y(i)= rand()
groupcoor=[x' y']
hold on
end
hold on
end
%New function on another script called randomcheck
function [CoordinateGroup]=randomcheck(N,Np)
%Where N=3 and Np=4
end
[x,y] =get_number(N);
hold on
for j=1:Np
CoordinateGroup=get_number(N)
x_end(j)=x(end)
hold on
end
groupcoor=[x' y']
The array groupcoor is never used after it is assigned to.
[x,y] =get_number(N);
Okay, that's good.
CoordinateGroup=get_number(N)
You are calling get_number(N) again, getting new random points, but you are discarding the y output of get_number() and assigning the x to CoordinateGroup . You never use CoordinateGroup after that.
x_end(j)=x(end)
Your call to get_number() in the loop is not assigning to x, so the x being referred to in x(end) is the x that was returned by the [x,y] = get_number(N) before the loop.
Dameon Solestro
Dameon Solestro le 27 Nov 2021
for the last one. how do i use the x's within the loop then??
x_end(j) = CoordinateGroup(end);
Dameon Solestro
Dameon Solestro le 27 Nov 2021
sorry, last question.. but is there also a way to get y_end(j) then??
Walter Roberson
Walter Roberson le 27 Nov 2021
I wrote before:
"You are calling get_number(N) again, getting new random points, but you are discarding the y output of get_number()"
Perhaps if you did not discard the y output of get_number() ?
Dameon Solestro
Dameon Solestro le 27 Nov 2021
but the first function, my outputs should be x,y (??) so then, how would y be kept?
[x_end(j), y_end(j)] = CoordinateGroup(end);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by