Info

This question is closed. Reopen it to edit or answer.

Check for most occurent result in every index of a string array, how? Mean of string array.

1 view (last 30 days)
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?

Answers (1)

Johannes Hougaard
Johannes Hougaard on 25 May 2020
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 Comments
Johannes Hougaard
Johannes Hougaard on 26 May 2020
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!