Assigning values from one array to string values of another array

5 views (last 30 days)
Hi! Im writing a script that is supposed to count votes for ideas (using 3 score categories) and then rank the ideas. The values are given by a csv file. My code currently imports the data and counts the scores for each idea (using a for loop) and stores the scores in a vector. It also counts the number of ideas that have come in, names them and stores the names as a string vactor. The vectors are the same length and the ideaname and scorevalue have the same indexes. To be able to rank the ideas I would need to connect each string value with a score number and then sort the ideas from high to low according to their scores. How do I do this?
If you happen to know neat ways to visuaise the results that would also be very valuable!
The vectors are 1x9 but the length of them are defined by a variable called NumIdeas, as I want the csv file to be editable. Currently they look like this:
ideanumber = ["idea1", "idea2", ..., "idea9"]
scorevector = [5.7065, 3.2885, 7.8947, 5.8056, 5.7619, 4.2955, 5.0740, 3.0227, 7.4564]
I tried using a table function (syntax: scoreboard = table(ideanumber, scorevector) ) but the following error message was displayed; "Error using () Unrecognised row name "idea1".
I am fairly new to Matlab and would greatly appreciate any guidance you can give :) Thank you!

Answers (2)

KSSV
KSSV on 15 Jul 2022
ideanumber = ["idea1", "idea2","idea3", "idea4","idea5", "idea6","idea7", "idea8", "idea9"] ;
scorevector = [5.7065, 3.2885, 7.8947, 5.8056, 5.7619, 4.2955, 5.0740, 3.0227, 7.4564] ;
T = table(ideanumber',scorevector')
T = 9×2 table
Var1 Var2 _______ ______ "idea1" 5.7065 "idea2" 3.2885 "idea3" 7.8947 "idea4" 5.8056 "idea5" 5.7619 "idea6" 4.2955 "idea7" 5.074 "idea8" 3.0227 "idea9" 7.4564

Stephen23
Stephen23 on 15 Jul 2022
Edited: Stephen23 on 15 Jul 2022
Are you trying to do something like this?
v = 1:9;
id = compose("idea%d",v(:));
sv = [5.7065; 3.2885; 7.8947; 5.8056; 5.7619; 4.2955; 5.0740; 3.0227; 7.4564];
tb = array2table(sv,'RowNames',id)
tb = 9×1 table
sv ______ idea1 5.7065 idea2 3.2885 idea3 7.8947 idea4 5.8056 idea5 5.7619 idea6 4.2955 idea7 5.074 idea8 3.0227 idea9 7.4564
tc = sortrows(tb)
tc = 9×1 table
sv ______ idea8 3.0227 idea2 3.2885 idea6 4.2955 idea7 5.074 idea1 5.7065 idea5 5.7619 idea4 5.8056 idea9 7.4564 idea3 7.8947
tb{'idea1',:}
ans = 5.7065

Community Treasure Hunt

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

Start Hunting!