필터 지우기
필터 지우기

Count Unique Occurrences of Elements

조회 수: 4 (최근 30일)
Patrick Rungrugeecharoen
Patrick Rungrugeecharoen 2019년 5월 26일
댓글: Patrick Rungrugeecharoen 2019년 5월 27일
I have the following vectors:
Stops = [w w x x x y z ] %String
Buses = [A B C A C D B] % String
UniqueBuses = [A B C D] %String
I would like to have an output which tells me that:
Stop w has 1 A, 1 B.
Stop x has 2 C, 1 A.
Stop y has 1 D.
Stotp z has 1B.
Any help would be appreciated thank you!

채택된 답변

per isakson
per isakson 2019년 5월 26일
편집: per isakson 2019년 5월 26일
Try this
%%
Stops = [ "w", "w", "x", "x", "x", "y", "z" ]; % String
Buses = [ "A", "B", "C", "A", "C", "D", "B" ]; % String
UniqueBuses = [ "A", "B", "C", "D" ]; % String
%%
UniqueStops = unique( Stops );
for stop = UniqueStops
ism = ismember( Stops, stop );
stopping_buses = Buses( ism );
str = compose( "Stop %s has", stop );
for bus = UniqueBuses
num = sum( double( ismember( stopping_buses, bus ) ) );
if num >= 1
str = compose( "%s %d %s,", str, num, bus );
end
end
disp( str )
end
it outputs
Stop w has 1 A, 1 B,
Stop x has 1 A, 2 C,
Stop y has 1 D,
Stop z has 1 B,
>>
  댓글 수: 3
per isakson
per isakson 2019년 5월 26일
편집: per isakson 2019년 5월 26일
Questions
  1. Stops, Buses and UniqueBuses are all of them rows, i.e not columns?
  2. Are you comfortable with Matlabs debugging tools? If not see Debug a MATLAB Program
Proposal
  1. Set Pause on Errors
  2. Run the script with your larger dataset
  3. When the execution halts before throwing the error inspect the values of str, num, bus. Does their dimensions agree? They shall all be <1x1>
Patrick Rungrugeecharoen
Patrick Rungrugeecharoen 2019년 5월 26일
To answer your question:
  1. The Stops, Buses and UniqueBuses are all vector columns actually. (Not familiar with MATLAB vector notation - sorry for the confusion).
The str, num and bus dimensions do not agree:
  1. str is a 1994 x 1 string.
  2. num is a 1 x 1 double.
  3. bus is a 251 x 1 string.
My larger data set was (if that helps):
  1. Stop is 5632 x 1 string.
  2. Buses is a 5632 x 1 string.
  3. UniqueBuses is a 251 x 1 string.
  4. UniqueStops is a 1994 x 1 string.
If it helps the output can just be a matrix form instead of string form?

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

추가 답변 (1개)

per isakson
per isakson 2019년 5월 26일
편집: per isakson 2019년 5월 26일
I cannot make comments, thus I use an Answer
To ensure that Stops, Buses and UniqueBuses are row vectors add the following lines in the top of the script
Stops = reshape( Stops, 1,[] );
Buses = reshape( Buses, 1,[] );
UniqueBuses = reshape( UniqueBuses, 1,[] );
"If it helps the output can just be a matrix form instead of string form?" Not needed. These reshape() statements should solve the problem. They output row vectors for both row and column vectors.
  댓글 수: 1
Patrick Rungrugeecharoen
Patrick Rungrugeecharoen 2019년 5월 27일
Thank you very much it works now.
Out of curiosity how would the code look if I wanted a header row consisting of UniqueBuses and the leading column to have UniqueStops then the matrix filled out with a bunch of numbers based on these results?

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

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by