Convert individual values in a matrix to letters and keep remaining as numbers

Hi,
I'm trying to fill in characters corresponding to elements in the array airline_names into the array Usage using the below code:
Usage = num2cell(CounterUsage);
airline_names = cellstr(['AC_ '; 'FI_ '; 'QR_ '; 'SK_C'; 'SQ_ '; 'STB '; 'STE '; 'TG_ ']);
for i=1:33
for j=1:288
if isequal(Usage(i,j),0) ;
else
Usage(i,j)=num2cell(double('AB'));
%else
%Usage(i,j) = num2str(airline_names(CounterUsage(i,j)));
end
end
end
Usage is a matrix of values from 0 to 8 where I want all non zero elements to be replaced by the corresponding element in airline_names, e.g. Usage(i,j) = 1 then 1 should be replaced by airline_names(1) = 'AC_ '
However, the above code doesn't work due to what I think might be some type/format errors..
Please help!:D
Best Regards Maria and Christian

 Accepted Answer

airline_names = cellstr(['AC_ '; 'FI_ '; 'QR_ '; 'SK_C'; 'SQ_ '; 'STB '; 'STE '; 'TG_ '])
usage=randi(8,8,1)
out=airline_names(usage)

3 Comments

In principle what I want to do, except that the usage matrix should contain both these airline_names and zeros on all other positions. This was why is tried looping through all elements in the usage matrix in other to check all of them a replace the values if they where not equal to zero. And furthermore I wanted to replace them with the corresponding element in the airline_names matrix.
So what I need is a matrix in which I can look up values (and a way to do so) and have both numbers and characters.
airline_names = cellstr(['AC_ '; 'FI_ '; 'QR_ '; 'SK_C'; 'SQ_ '; 'STB '; 'STE '; 'TG_ '])
usage=randi(8,288,33)-1
idx=find(usage==0)
idx1=setdiff(1:numel(usage),idx)
out=cell(size(usage))
out(idx)=num2cell(0)
out(idx1)=airline_names(usage(idx1))

Sign in to comment.

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!