apply text values to elements in a vector
7 ビュー (過去 30 日間)
古いコメントを表示
I have a vector that is a sequence of number and NaN values. Is there a way for me to save values for these numbers and NaN values and then have the entire vector display with those text values inserted? For example:
If my vector is the column on the left below, and my variables are defined as
5 = turn left
4 = turn right
6 = go straight
NaN = not moving
can I get it to display as the column on the right after saving the variables?
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
NaN not moving
5 turn left
Thanks in advance for the help!
0 件のコメント
採用された回答
Cedric
2013 年 10 月 30 日
編集済み: Cedric
2013 年 10 月 30 日
If you are allowed to remap "not moving" to e.g. code 1, you can proceed as follows:
actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
labels = {'not moving', '', '', 'turn right', 'turn left', 'go straight'} ;
% - NaN -> 1.
actions(isnan(actions)) = 1 ;
% - Display.
for k = 1 : numel( actions )
fprintf( '%d\t%s\n', actions(k), labels{actions(k)} ) ;
end
With that, you get
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
1 not moving
5 turn left
If you don't want that remap (NaN->1), it is easy to use an IF statement in a loop for managing NaN entries..
actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
labels = {'turn right', 'turn left', 'go straight'} ;
for k = 1 : numel( actions )
if isnan( actions(k) )
fprintf( 'NaN\tnot moving\n' ) ;
else
fprintf( '%d\t%s\n', actions(k), labels{actions(k)-3} ) ;
end
end
With that, you get
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
NaN not moving
5 turn left
6 件のコメント
Cedric
2013 年 10 月 31 日
You could generate a long string using SPRINTF ..
actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
labels = {'not moving', '', '', 'turn right', 'turn left', 'go straight'} ;
actions(isnan(actions)) = 1 ;
temp = [num2cell(actions); labels(actions)] ;
report = sprintf( '%d\t%s\n', temp{:}) ;
With that, what was outputted to screen previously is now stored as a string in variable report, that you can display by typing its name in the command window.
A better option (up to me) would be to pack the code for printing the report in a function, and call the function when you want the report. This way you can easily get a dynamic report if actions evolve.
function displayReport( actions )
labels = {'not moving', '', '', 'turn right', 'turn left', ...
'go straight'} ;
actions(isnan(actions)) = 1 ;
for k = 1 : numel( actions )
fprintf( '%d\t%s\n', actions(k), labels{actions(k)} ) ;
end
end
Saving this in file displayReport.m, you can then call it passing actions from the command window at any time (or from your script)..
>> actions = [5,5,5,4] ;
>> displayReport( actions ) ;
5 turn left
5 turn left
5 turn left
4 turn right
and a moment later, with more actions having stacked in your actions buffer ..
>> actions = [5,5,5,4,4,4,6,6,6,NaN,5] ;
>> displayReport( actions ) ;
5 turn left
5 turn left
5 turn left
4 turn right
4 turn right
4 turn right
6 go straight
6 go straight
6 go straight
1 not moving
5 turn left
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!