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

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?

Antworten (1)

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 Kommentare

Sorry - I can't seem to understand the case. Would it be possible for you to attach the function you're using (or to insert a code snippet that generates your data)?
I think the right way to go is to use the function 'unique' and focus on the third output.
Unfortunately your 'generate_secret_data' function was not included.
I think it could be done with the third output from unique- however 'mode' seems to be more correct.
If I've understood your data correct It'd be something like
result1 = [ "3" "3" "A" "3"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "*" "7" "2"
"9" "1" "9" NaN];
result2 = [ "3" "3" "A" "3"
"A" "5" "5" "B"
"C" "C" "2" "B"
"4" "*" "7" "2"
"9" "1" "9" NaN];
result3 = [ "4" "3" "A" "3"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "*" "7" "2"
"9" "1" "9" NaN];
result4 = [ "3" "3" "A" "3"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "*" "7" "2"
"9" "2" "7" NaN];
result5 = [ "3" "3" "G" "3"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "*" "7" "2"
"7" "1" "9" NaN];
result6 = [ "3" "3" "A" "2"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "*" "7" "3"
"9" "1" "9" NaN];
result7 = [ "1" "3" "A" "3"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "*" "8" "2"
"9" "1" "9" NaN];
result8 = [ "3" "3" "A" "3"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "+" "7" "2"
"9" "1" "9" NaN];
result9= [ "3" "3" "A" "3"
"A" "5" "5" "B"
"C" "3" "4" "B"
"4" "*" "7" "2"
"9" "1" "9" NaN];
result10 = [ "3" "3" "A" "3"
"A" "5" "5" "B"
"C" "3" "2" "B"
"4" "*" "7" "2"
"9" "1" "9" NaN];
If that's the case your issue can be handled by storing the multiple results as slabs in a m-by-n-by-o three dimensional array and finding the most common entry as described below:
for ii = 1:10
c = eval(strcat('result',num2str(ii)));
overallresult(:,:,ii) = c;
end
clear c ii
n = size(overallresult,1);
m = size(overallresult,2);
for rowid = 1:n
for colid = 1:m
[slabvalue,~,slaborder] = unique(overallresult(rowid,colid,:));
mostcommonvalue(rowid,colid) = slabvalue(mode(slaborder));
end
end
Rather than awkwardly and inefficiently forcing meta-data (e.g. pseudo.indices) into variable names, the simple and efficient MATLAB approach is to use actual indexing (e.g .into a cell array).
"I'm not really in a position to care if my code is efficient or good"
Note that "efficient" is not just measured in runtime, but also in coding time, debugging time, maintenance time, and time waiting for random strangers on the internet to help you with basic tasks (i.e. your time).
"...and i would rather not rewrite a lot of stuff."
Ummm... I don't see any reason why you would need to "rewite a lot of stuff". Why do you think this?
Clearly whatever way you choose to store your data you will have to write something at some point in your code to store that data, and the two methods being discussed here would both require to be invoked at the same point in your code. So you have a choice of one simple method and one complex method in exactly the same location in your code: if one of them does not require that you "rewite a lot of stuff", then the other one does not either. There is absolutely zero difference between them from that point of view.
Yeah i watched some videos, you're probably right so i'll check it out.
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.

Diese Frage ist geschlossen.

Gefragt:

am 25 Mai 2020

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by