I am simplifying coordinates, MwCoords. The left column is the x values and the right column is y values. I do not want to alter the postition of the x or y values.
MwCoords =
241.5493 360.1065
241.7508 251.1975
246.9076 148.8279
350.6785 144.9718
349.2095 255.6598
352.6571 364.5513
456.4168 149.5675
459.3270 256.3704
460.4547 360.4354
I have been using mink to find the three minimum values in the x and maxk to find the 3 max values in the x
MwCoordsMins = mink(MwCoords(:,1),3)
MwCoordsMaxs = maxk(MwCoords(:,1),3)
This outputs
MwCoordsMins =
241.5493
241.7508
246.9076
MwCoordsMaxs =
460.4547
459.3270
456.4168
I need to put 1's in the mins, and 3's in the max of the origional MwCoords
From there I need to assign the 3 middle values to 2's
Then I need to plug it back into the origional matrix without disrupting the positioning of these. The postitioning will change every now and then, thats why it has to be in the same place.
After I figure out the x values, I move on to the y values.
If anyone can help, or offer insight that would be great!
Edit - Whatever I added, I put it in bold

댓글 수: 2

dpb
dpb 2021년 2월 8일
Return the optional location vector from mink, maxk. Then it's trivial to replace those locations with a constant.
The combination of all these operations if going to be done repeatedly would be a prime candidate for a user function to operate on the array passed to it.
Conner Carriere
Conner Carriere 2021년 2월 8일
I guess I am not sure how to return the outputs to their origional locations. It will be done repeatadly, so I will attempt to make a function out of it.

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2021년 2월 8일
편집: Star Strider 2021년 2월 8일

1 개 추천

The mink and maxk functions can give the indices of those values as the second output.
If I understand correctly what you want to do, I would do something like this:
[MwCoordsMins,ix1] = mink(MwCoords(:,1),3);
[MwCoordsMaxs,ix2] = maxk(MwCoords(:,1),3);
Then assign the elements accordingly as ‘1’, ‘2’, or ‘3’.
EDIT — I originally posted the full answer before noticing that this was tagged as homework. That part is now gone.
.

댓글 수: 9

This is for my final project that is not due for a bit. I guess I am a bit confused how to assign certain parts of a matrix. Also I do not understand why the ix1 outputs 1,2,3 when there are 9 variables that it is reading.
[MwCoordsMins,ix1] = mink(MwCoords(:,1),3);
ix1 = 1
2
3
If it’s not actually homework, I’ll post the rest of it:
I need to put 1's in the mins, and 3's in the max of the origional MwCoords
From there I need to assign the 3 middle values to 2's
So:
[MwCoordsMins,ix1] = mink(MwCoords(:,1),3);
[MwCoordsMaxs,ix2] = maxk(MwCoords(:,1),3);
MwCoords(:,1) = 2;
MwCoords(ix1,1) = 1;
MwCoords(ix2,1) = 3;
produces:
MwCoords =
1 360.11
1 251.2
1 148.83
2 144.97
2 255.66
2 364.55
3 149.57
3 256.37
3 360.44
I’m not certain what you want to do with the rest, so I’ll leave that to you.
Conner Carriere
Conner Carriere 2021년 2월 8일
Thank you for your help!
Star Strider
Star Strider 2021년 2월 8일
As always, my pleasure!
Sorry to bother, but I got everthing working, but some issue pops up and im unlcear why.
I am creating a function for this
function MCoords = CoordsMinMax(g)
%This is for the x values
[MwCoordsMinsx,ix1] = mink(g(:,1),3);
[MwCoordsMaxsx,ix2] = maxk(g(:,1),3);
MCoords(:,1) = 2;
MCoords(ix1,1) = 1;
MCoords(ix2,1) = 3;
%this is for the y values
[MCoordsMinsy,iy1] = mink(g(:,2),3);
[MCoordsMaxsy,iy2] = maxk(g(:,2),3);
MCoords(:,2) = 2;
MCoords(iy1,2) = 1;
MCoords(iy2,2) = 3;
end
This is how I call it in the script
Mw2Coords = CoordsMinMax(MwCoords)
It is just called Mw2Coords now, so I dont overwrite MwCoords while working on it.
Here is the original Mw Coords
MwCoords =
241.5493 360.1065
241.7508 251.1975
246.9076 148.8279
350.6785 144.9718
349.2095 255.6598
352.6571 364.5513
456.4168 149.5675
459.3270 256.3704
460.4547 360.4354
The output of the function is
Mw2Coords =
1 3
1 2
1 1
0 1
0 2
0 3
3 1
3 2
3 3
Why do those 3 0's come up?
I’m not exactly certain what the problem is since I didn’t see the function call itself.
This code (with your original matrix):
[MwCoordsMins,ix1] = mink(MwCoords(:,1),3);
[MwCoordsMaxs,ix2] = maxk(MwCoords(:,1),3);
MwCoords(:,1) = 2;
MwCoords(ix1,1) = 1;
MwCoords(ix2,1) = 3;
[MCoordsMinsy,iy1] = mink(MwCoords(:,2),3);
[MCoordsMaxsy,iy2] = maxk(MwCoords(:,2),3);
MwCoords(:,2) = 2;
MwCoords(iy1,2) = 1;
MwCoords(iy2,2) = 3;
produces this result:
MwCoords =
1 3
1 2
1 1
2 1
2 2
2 3
3 1
3 2
3 3
.
dpb
dpb 2021년 2월 8일
편집: dpb 2021년 2월 9일
function MCoords = CoordsMinMax(g)
% Replace 3 min, max X,Y coordinates --> 1,3 respectively; remainder --> 2
[~,imnx] = mink(g(:,1),3); % indices min 3 in X
[~,imxx] = maxk(g(:,1),3); % indices max 3 in X
[~,imny] = mink(g(:,2),3); % ditto for Y
[~,imxy] = maxk(g(:,2),3);
MCoords=2*ones(size(g)); % set output to 2 same size as input
MCoords(imnx,1) = 1; % minima are 1
MCoords(imxx,1) = 3; % maxima are 3
MCoords(imny,1) = 1; % ditto
MCoords(imxy,1) = 3;
end
Conner Carriere —
Put this assignment as the first line in your function, the problem then disappears, and the output is correct:
MCoords = g;
(Note that dpb’s solution does essentially the same thing.)
dpb
dpb 2021년 2월 9일
NB: original had column 1 in all; now fixed above -- dpb

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

질문:

2021년 2월 8일

댓글:

dpb
2021년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by