Info
此问题已关闭。 请重新打开它进行编辑或回答。
Check for most occurent result in every index of a string array, how? Mean of string array.
1 次查看(过去 30 天)
显示 更早的评论
Let's say that i have a code that returns a 3x3 string array, the strings that can occurr are A,B,C,D,E,F,G,H,I so 9 total values. Most of the time, the code returns the "right" answer, let's say that that is A,A,A,B,B,B,C,C,C but due to differences in randomly generated noise the result is sometimes off, meaning that something like A,A,G,B,B,B,C,D,C could happen. I'm thinking i could make a loop that checks the "average" string array so that if you increase the amount of times the loop is run the probability of obtaining the correct array increases as well. But i don't know what to use, the only things i've found is how to check for the most occurent value in an array and how to return that, but i want to get kind of an "average" array, how could i do it?
0 个评论
回答(1 个)
Johannes Hougaard
2020-5-25
I'm not completely sure I get the question, but in theory you could simply convert the char array to numbers and revert to characters
str = ['A' 'A' 'A' 'B' 'B' 'B' 'C' 'C' 'C' 'D' 'D' 'D'];
num = double(char(str(:)));
avg_str = char(mean(num)); %'B'
str = ['A' 'A' 'G' 'B' 'B' 'B' 'C' 'D' 'C' 'D' 'D' 'F'];
num = double(char(str(:)));
avg_str = char(mean(num)); %'C'
This seems to do something of what you wish.
Otherwise I'd take a look at the documentation for 'unique' that might help you.
6 个评论
Johannes Hougaard
2020-5-26
Agreed. The 'eval' option is hideous - It was an appaling 'quick-fix' proposed by me.
The better way to do it is to assign your output 'result' directly to the 3rd dimension of the array in a for loop. I just wasn't able to do that without writing more code than I had the time to do without having your 'generate_secret_data' function.
I'm sorry I'm not able to help you.
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!