Need help fixing simple function

조회 수: 3(최근 30일)
Conner Carriere
Conner Carriere 2021년 2월 11일
I have a function that simplifies coordinates and colors
The part that edits the coordinates is correct, but the colors is not
Here is the function
%function positionsnew = CubeLayout(RawCoords, Colors)
%Pre allocates newcoords
newcoords = zeros(9,1);
RawCoords = MrCoords; %JUST HAVE THIS AT THE MOMENT TO TEST
%This determines the position of the coordinates
for k = 1: size(RawCoords,1)
if RawCoords(k,1) == 1 && RawCoords(k,2) == 1
newcoords(k,:) = 1;
elseif RawCoords(k,1)== 1 && RawCoords(k,2)== 2
newcoords(k,:) = 2;
elseif RawCoords(k,1)== 1 && RawCoords(k,2)== 3
newcoords(k,:) = 3;
elseif RawCoords(k,1)== 2 && RawCoords(k,2)== 1
newcoords(k,:) = 4;
elseif RawCoords(k,1)== 2 && RawCoords(k,2)== 2
newcoords(k,:) = 5;
elseif RawCoords(k,1)== 2 && RawCoords(k,2)== 3
newcoords(k,:) = 6;
elseif RawCoords(k,1)== 3 && RawCoords(k,2)== 1
newcoords(k,:) = 7;
elseif RawCoords(k,1)== 3 && RawCoords(k,2)== 2
newcoords(k,:) = 8;
elseif RawCoords(k,1)== 3 && RawCoords(k,2)== 3
newcoords(k,:) = 9;
end
end
%Colors second part of function
Colors = MrC; %JUST HAVE THIS AT THE MOMENT TO TEST
%reshapes position vector and combines newcoords with Color
positions = ([1 2 3, 4 5 6, 7 8 9]);
positions = reshape(positions,3,3);
ColorLocation = [newcoords,Colors];
%sets the color vector in a new 3x3 matirx
positionsnew = zeros(3,3);
for i = 1:9
for j = 1:9
if ColorLocation(i,1) == j %if colorlocations column 1 row 1 is equal to 1 then,
positionsnew(positions == i) = ColorLocation(i,2);
end
end
end
%end
MrCoords is this
MrCoords =
1 3
1 2
1 1
2 2
2 3
2 1
3 1
3 3
3 2
Again, that part of the code is fine, and does not need adjusting, it outputs newcoords, which is a 9x1 matrix
MrC is this
MrC =
4
5
5
1
2
1
5
6
6
This is what I need done
ColorLocation combines newcoords and colors, this outputs a 9x2 matrix, the left side being a position, and the right side being a color.
I need to put the respective color in the location of the positions matrix, which is a 3x3
for example, positionsnew is the color output of positions and color location
positions =
1 4 7
2 5 8
3 6 9
ColorLocation =
3 4
2 5
1 5
5 1
6 2
4 1
7 5
9 6
8 6
positionsnew=
5 1 5
5 1 6
4 2 6
Please let me know if I can clear anything up,

답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by