- MrCoords(:, 1) - 1: Subtracting 1 shifts the row index to start from 0 (0-based indexing) instead of 1 (MATLAB's 1-based indexing).
- (MrCoords(:, 1) - 1) * 3: Multiplies the adjusted row index by the number of columns (3) in the grid. This gives the starting position for that row within the linear numbering.
- + MrCoords(:, 2): Adds the column index to determine the exact position within the row. Since column numbers (1, 2, 3) directly map to positions within the row, this completes the calculation.
Need help fixing simple function
22 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Anaghaa
il 10 Feb 2025 alle 6:56
Hi, I went through the function used and found that the issue lies in the for loop of the code. The inner loop (for j = 1:9) is not necessary since each position is already uniquely defined in “ColorLocation(:, 1)”. The line “positionsnew(positions == i) = ColorLocation(i, 2)” misuses “i”, which is the loop index, not the actual position in the grid. This causes a mismatch, as positions contains grid numbers (1 to 9), not indices.
Here is a solution to map the colours correctly:
% Map coordinates to positions using formula: Position = (Row – 1) x 3 + Column
newcoords = (MrCoords(:, 1) - 1) * 3 + MrCoords(:, 2);
% Combine positions and colors
ColorLocation = [newcoords, MrC];
positions = reshape(1:9, 3, 3);
positionsnew = zeros(3, 3);
% Map colors to positionsnew
for i = 1:size(ColorLocation, 1)
[row, col] = find(positions == ColorLocation(i, 1));
positionsnew(row, col) = ColorLocation(i, 2);
end
% Display output
disp(positionsnew);
The corrected code maps colors to the exact grid positions using “positions == ColorLocation(i, 1)” inside the “find” function.
For reference, below is the link for “find” function:
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!