Finding elements in column that meet some criteria

조회 수: 1 (최근 30일)
djr
djr 2014년 9월 9일
댓글: dpb 2014년 9월 10일
Hello,
I have a matrix that has 3 columns and 186 rows. Values are something like this:
Velocity X Y
6.73568234861034 0 500
6.73568234861034 2000 520
6.73568234861034 -2000 540
6.73568234861034 4000 560
6.76077918572001 0 580
6.76071354732644 6000 600
I need to transform this into matrix that will be like this
X Z Velocity
X and Z have to start from 0 and go up to maximum (8000 for x and 600 for z). This was easy. I used "sort" function, so I took care of X and Z columns.
However, now I need to figure out how to copy "Velocity" values from the first table into the second, but in a way that "Velocity" values correspond to the right value of X and Z. I guess I could use "for" loops with "if" statements, but I was wondering is there any other (easier) way; just using built-in functions?
Could you please help me with this?
Thanks, DjR
  댓글 수: 1
per isakson
per isakson 2014년 9월 9일
편집: per isakson 2014년 9월 9일
  • Are you aware of the second output of sort?
  • You cannot sort on both X and Y at the same time - what do i miss?
  • The words "matrix" and "table" are they synonyms?
  • ... or table is that table, Create table from workspace variables

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

채택된 답변

dpb
dpb 2014년 9월 9일
편집: dpb 2014년 9월 10일
M=sortrows([m(:,2:3) m(:,1)],[1 2]);
Presuming m is original array w/o the header line and your "Z" is really "Y"
Or, the explicit column rearrangement can be written in a "more Matlaby" manner, and the result as
M=sortrows(circshift(m,[0 -1]),[1 2]);

추가 답변 (1개)

djr
djr 2014년 9월 10일
Thanks so much. Yes, Z is Y (my bad).
  댓글 수: 1
dpb
dpb 2014년 9월 10일
You're welcome...always a challenge to find the proper function(s) when first becoming familiar with Matlab. NB:
lookfor sort
would have located sortrows for you altho it's listed in the "friends" category of See Also under sort. Always look into the alternatives therein if the base function doesn't quite fit as here. The 'rows' part of the name given your requirements should've been a hint.
circshift isn't quite so easy, granted... :)

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by