Sort all dimensions of a cell array from min to max in MATLAB - matlab

I have created a cell array called Items of size (10,5). It contains the following as the first column in the cell array:
Item name Price (in $) Shipping (in $) Total price (in $) Total price (in €)
I have it all filled up, but what I need to do is to sort the cell array according to the total price in € from the smallest to the largest, but I can't seem to find a way to do it. I tried sort(Items,5); in order to sort according to the values in € but it returns an error. It could be useful to find a way to make the sorting automatic, so if I wanted to add more items it would still sort them in the global list.

sortrows will likely do exactly what you want to do. It will sort based on a specific column, assuming the datatype is constant in the entire column.
>> a ={'a',8,9;'b',5,6;'c',2,3};
>> a_sorted = sortrows(a,3)
a_sortred =
'c' [2] [3]
'b' [5] [6]
'a' [8] [9]
Edit
From your comments below, you can easily just sort the array first and then add a row to the cell array the same way you would combine regular arrays. Documentation
>> a = {7,8,9;4,5,6;1,2,3};
>> a_sorted = sortrows(a,3);
>> headers = {'col1','col2','col3'};
>> a_header = [headers;a_sorted]
a_header =
'col1' 'col2' 'col3'
[ 1] [ 2] [ 3]
[ 4] [ 5] [ 6]
[ 7] [ 8] [ 9]
EDIT #2
You can round the values that you are presenting using the second argument of the round function. After you round it, you can change the format of how things are displayed. Normally it is set as short, which is 4 decimal places. If you set it to shortg it will show as few decimals as possible, up to 4.
>> a = [1.23456789;2.3456789;3.456789]
a =
1.2346
2.3457
3.4568
>> a_rounded = round(a,2)
a_rounded =
1.2300
2.3500
3.4600
>> format shortg
>> a_rounded
a_rounded =
1.23
2.35
3.46
If changing the format is not an option, you could always just convert the number into a string and then display that. That gets a little more complicated, but a quick google will help you there.
EDIT #3
I did not know this existed before, but you can apparently use the format style called bank. This will display all numbers as two decimal points even if they are 0.

First place all of the prices in a separate array, sort on this array individually then use the indices of sorting to rearrange the rows of your cell array.
Try something like this:
price = [Items{:,5}];
[~,ind] = sort(price);
ItemsSorted = Items(ind,:);
Alternatively you can use the sortrows function that MZimmerman6 mentioned and operate along the fifth column of your cell array. I wasn't aware it worked on cell arrays, so I learned something new!

Related

I'm having trouble shuffling a deck of cards in Matlab. Need help to see where I went wrong

I currently have the deck of cards coded, but it is unshuffled. This is for programming the card game of War if it helps. I need to shuffle the deck, but whenever I do, it will only shuffle together the card numbers and the suits, not the full card. For example, I have A identified as an ace and the suits come after each number. A normal card would be "AH" (an ace of hearts) or "6D" (a six of diamonds). Instead, it will output "5A" as one of the cards, as in a 5 of aces. I don't know how to fix this, but the code that I currently have is this:
card_nums = ('A23456789TJQK')';
card_suits = ('HDSC')';
unshuffled_deck = [repmat(card_nums,4,1),repmat(card_suits,13,1)];
disp(unshuffled_deck)
shuffled_deck = unshuffled_deck(randperm(numel(unshuffled_deck)));
disp(shuffled_deck)
I would appreciate any help with this, and thank you very much for your time!
You're creating a random permutation of all of the elements from both columns of unshuffled_deck combined. Instead you need to create a random permutation of the rows of unshuffled_deck:
shuffled_deck = unshuffled_deck(randperm(size(unshuffled_deck,1)),:);
The call to size gives you the number of rows in the deck array, then we get a random permutation of the row indices, and copy the row (value, suit) as a single entity.
Here's a version using a structure array in response to #Carl Witthoft's comment. I was afraid it would add too much complexity to the solution, but it really isn't bad:
card_nums = ('A23456789TJQK')';
card_suits = ('HDSC')';
deck_nums = repmat(card_nums,4,1);
deck_suits = repmat(card_suits,13,1);
cell_nums = cellstr(deck_nums).'; %// Change strings to cell arrays...
cell_suits = cellstr(deck_suits).'; %// so we can use them in struct
%// Construct a struct array with fields 'value' and 'suit'
unshuffled_deck = struct('value',cell_nums,'suit',cell_suits);
disp('unshuffled deck:');
disp([unshuffled_deck.value;unshuffled_deck.suit]);
%// Shuffle the deck using the number of elements in the structure array
shuffled_deck = unshuffled_deck(randperm(numel(unshuffled_deck)));
disp('shuffled deck:');
disp([shuffled_deck.value; shuffled_deck.suit]);
Here's a test run:
unshuffled deck:
A23456789TJQKA23456789TJQKA23456789TJQKA23456789TJQK
HDSCHDSCHDSCHDSCHDSCHDSCHDSCHDSCHDSCHDSCHDSCHDSCHDSC
shuffled deck:
4976TT93KTJQJATK953A75QA82Q6226K5J784J4A3372486K859Q
CHSSSHCDSCSSHDDCDSHHCDHSDDCDHCCHHCHHHDDCSCDSSCHDSCSD
To access an individual card, you can do:
>> shuffled_deck(2)
ans =
scalar structure containing the fields:
value = 9
suit = H
Or you can access the individual fields:
>> shuffled_deck(2).value
ans = 9
>> shuffled_deck(2).suit
ans = H
Unfortunately, I don't know of any way to simply index the struct array and get, for instance, 9H as you would in a regular array using disp(shuffled_deck(2,:)). In this case, the only option I know of is to explicitly concatenate each field:
disp([shuffled_deck(2).value,shuffled_deck(2).suit]);

find value in a string of cell considering some margin

Suppose that I have a string of values corresponding to the height of a group of people
height_str ={'1.76000000000000';
'1.55000000000000';
'1.61000000000000';
'1.71000000000000';
'1.74000000000000';
'1.79000000000000';
'1.74000000000000';
'1.86000000000000';
'1.72000000000000';
'1.82000000000000';
'1.72000000000000';
'1.63000000000000'}
and a single height value.
height_val = 177;
I would like to find the indices of the people that are in the range height_val +- 3cm.
To find the exact match I would do like this
[idx_height,~]=find(ismember(cell2mat(height_str),height_val/100));
How can I include the matches in the previous range (174-180)?
idx_height should be = [1 5 6 7]
You can convert you strings into an numeric array (as #Divakar mentioned) by
height = str2num(char(height_str))*100; % in cm
Then just
idx_height = find(height>=height_val-3 & height<=height_val+3);
Assuming that the precision of heights stays at 0.01cm, you can use a combination of str2double and ismember for a one-liner -
idx_height = find(ismember(str2double(height_str)*100,[height_val-3:height_val+3]))
The magic with str2double is that it works directly with cell arrays to get us a numeric array without resorting to a combined effort of converting that cell array to a char array and then to a numeric array.
After the use of str2double, we can use ismember as you tried in your problem to get us the matches as a logical array, whose indices are picked up with find. That's the whole story really.
Late addition, but for binning my first choice would be to go with bsxfun and logical operations:
idx_height = find(bsxfun(#le,str2double(height_str)*100,height_val+3) & ...
bsxfun(#ge,str2double(height_str)*100,height_val-3))

Is there a quick way to assign unique text entries in an array a number?

In MatLab, I have several data vectors that are in text. For example:
speciesname = [species1 species2 species3];
genomelength = [8 10 5];
gonometype = [RNA DNA RNA];
I realise that to make a plot, arrays must be numerical. Is there a quick and easy way to assign unique entries in an array a number, for example so that RNA = 1 and DNA = 2? Note that some arrays might not be binary (i.e. have more than two options).
Thanks!
So there is a quick way to do it, but im not sure that your plots will be very intelligible if you use numbers instead of words.
You can make a unique array like this:
u = unique(gonometype);
and make a corresponding number array is just 1:length(u)
then when you go through your data the number of the current word will be:
find(u == current_name);
For your particular case you will need to utilize cells:
gonometype = {'RNA', 'DNA', 'RNA'};
u = unique(gonometype)
u =
'DNA' 'RNA'
current = 'RNA';
find(strcmp(u, current))
ans =
2

MatLab: Find numeric values in cell array

In my cell array test = cell(1,2,20,14); I want to find numeric values in the subset test(:,1,1,1).
For example test(:,:,1,1) looks like this:
>> test(:,:,1,1)
ans =
[ 0] [0.1000] [57]
[0.9000] [0.9500] [73]
I want to find the index of the cell containing 0.9 in the first column, so I can access the third column (in this case value 73). I tried:
find(test{:,:,1,1} == 0.9) which gives:
Error using == Too many input arguments..
How can I find the respective index?
Thanks.
Try this to access that third column value directly -
cell2mat(test(vertcat(test{:,1,1,1})==0.9,3,1,1))
Edit 1: If you would like to test out for match w.r.t. the first two columns of test's subset, use this -
v1 = reshape(vertcat(test{:,[1 2],1,1}),[],2)
cell2mat(test(ismember(v1,[0.9 0.95],'rows'),3,1,1))
Just add brackets [] around test{:,:,1,1}. This wraps the different cell values together to one vector/matrix.
Like this:
[index1, index2] = find([test{:,:,1,1}] == 0.9)

Searching cell array with regex

I often find myself trying to search cell arrays like I would want to search a database with a sql query. In this case, I've got a number of military bases (bases.shp)
bases = shaperead('us-military-bases.shp')
and then I want to filter down the shape file to get Air Force bases, something like regexp({bases.FAC_NAME}','Air Force'). But the output I get is the fairly cumbersome:
[]
[]
[ 4]
[]
[]
[ 9]
[]
I am sure filtering down cell arrays or shapefiles is pretty common and there have to be some good practices. Thanks for any insight.
I am also trying things like:
trif = arrayfun(#(x)regexp(x.FAC_NAME,'Griff','match'),af_bases)
Given the output of regexp you can index back into the original cell array just by checking if each item in the resultant cell array is empty. You can do this using cellfun to apply a function to each cell.
To get an array of logicals, for non-empty items you can do:
base_strings = {bases.FAC_NAME}';
ind = ~cellfun(#isempty, regexp(base_strings, 'Air Force'))
Or more cleanly using an anonymous function:
ind = cellfun(#(x)( ~isempty(x) ), regexp(base_strings, 'Air Force'))
Then, to filter:
filtered = base_strings(ind);