How do I assign a number to an image in a cell array?

Hi guys!
I have a 1x40 cell array with 40 images ({'1.jpg'},{'2.jpg'}...and so forth). And I want to assign certain numbers to certain images because these numbers stand for certain chracteristics of the images (gender, identity, facial expression....). I need these numbers for a randomization.
So, does somebody know how to assign numbers to the images of a cell array?
Thanks in advance!
Best,
R

1 comentario

KSSV
KSSV el 8 de Oct. de 2020
Simple..you define the numbers in a 1*40 array. Where each index corresponds to respetive image.

Iniciar sesión para comentar.

Respuestas (1)

Jon
Jon el 8 de Oct. de 2020
Editada: Jon el 8 de Oct. de 2020
If you have a number of characteristics and don't want to have some elaborate encoding for how these are all specified I would put the data into a table with a column for the images (your current cell array) and then additional columns for each characteristic of interest. You could then easily access the data according to the rows that have the charactertics that match the ones of interest. Type doc table on the command line for details about how to create a table. Here is an overview of using tables https://www.mathworks.com/help/matlab/matlab_prog/advantages-of-using-tables.html
Another way to do this, if you just had a single characteristic encoded as a number to go with each image would be just to make a vector with the same number of elements as your cell array and store the characteristics in the elements of this vector. Lets call the vector of characteristics, imageCharacteristics, and the cell array of image, images. Then if you wanted for example to set the imageCharacteristics corresponding to the 4th image in your cell array to a value of 232 (just making up something for the example) you could assign
imageCharacteristics(4) = 232
later if you wanted the image and its characteristics you could open up
image = images{4}
imageCharacteristic = imageCharacteristic(4)
Still I would recommend putting the data into a table as a good general approach

8 comentarios

Thank you very much for your answer, Jon!
I created a table with all the characteristics for the first 2 images.
2×6 table
image identity con/incon gender facial expression age
_________ ________ _________ ______ _________________ ___
{'1.jpg'} 1 1 1 1 1
{'2.jpg'} 1 0 1 1 1
Now I wonder, how can I compare these characteristics of an image with a numeric value?
For example, to code something like: "If identity of first image in the sequence == 1 then identity of second image ~= 1", when randomizing the presentation sequence.
Best,
R
Jon
Jon el 13 de Oct. de 2020
I'm sorry, but I'm not understanding your follow up question. Can you please try to explain what you are doing further. Also, I'm wondering about the values you have in your columns. It looks like they are all binary (true/false ie 0 1). I can somewhat imagine this makes sense for gender, assuming for example that true means male and false means not male. I have a harder time understanding how age would be binary exce[t something like over 50 years old, not over 50 years old. Is this really what you are intending? Also what is the meaning of a 1 or 0 in the column called identity, or can that take on other values than 1 and 0 (you just show 1 for both rows).
Thank you for your quick response, Jon!
First about the values in the columns: "identity" reaches from 1 to 10 and refers to the person seen on the image. "con/incon" is binary, 1 means congruent and 0 means incongruent (facial expression congruent/incongruent to the text written across the image). "gender" is clear, "facial expression" is binary as well, 0 for fearful and 1 for happy. "age" refers to th age group and reaches from 1 to 3 (young, middle-aged, adult).
I'm sorry for choosing the first two images and their characteristics, since the value distribution of 1 and 0 can indeed be confusing.
Now about what we are trying to do further: We need a pseudo-randomization of the presentation sequence of the images. There are several factors to consider. We need an equal distribution of gender, age and facial expression. Furthermore, an image with person X must not follow or precede an image with the same person (therefore we need to know the identity). And finally, we need an equal number of con-con, incon-con, incon-incon and con-incon stimulus pairings. For example, if a congruent image follows or precedes another congruent one, that would be a con-con pairing.
So, in order to achieve this pseudo-randomization we need to access the images characteristics. Our current approach is, that we choose the images of the sequence randomly one by one. And from the second image onwards they have to pass some if-conditions. For example: If identity of first image in the sequence == 1 then identity of second image ~= 1. And so forth.
Jon
Jon el 14 de Oct. de 2020
Sorry, I haven't had a chance to look deeper into this. Busy at moment but maybe later this week. When you say you need an equal number of con-con, incon-con etc do they actually have to be equal, or just uniformly distributed, like the age, gender etc?
Nevermind, thank you for your support, Jon!
With an equal distribution of age, gender an so forth I meant, that we need the same number of all different stimuli within one feature. For example, if we have 200 stimuli presentations, we need 100 male and 100 female stimuli.
And if we have 25 con-con parings, we need 25 con-incon, incon-incon and incon-con pairings as well.
Sorry, I phrased that vaguely.
Jon
Jon el 16 de Oct. de 2020
Editada: Jon el 16 de Oct. de 2020
It's not obvious to me how you would generate "random" sequences of con,incon pairings, such that you had exactly the same number of each. The difficulty that I see is that you can't just have a bucket of say 25 con-incon, 25 con-con, 25 incon-con and 25 incon-incon possibilities and then just randomly draw from the bucket until they are exhausted. The problem in doing this is that certain pairing can only occur following other pairings. For example if on the nth presentation we had a con-con pairing then on the n+1 presentation we could only have a con-con or a incon-con (Here I assume your terminology is to list the current presentation type followed by the previous presentation type).
So I don't think I can help you much on developing your actual methodology. Once you get to the point that you have defined exactly what you want to do (you more or less have an algorithm) but are having problems determining how to implement that in MATLAB I (and I'm sure many other contributors) could help you further.
You are right, it is a pseudo-random sequence.
The idea is that we are keeping track of the stimulus pairings (con-con and so forth), the age, the gender, the facial expressions and the identity of the stimuli. So we can tell Matlab to draw again from the stimuli pool if the drawn stimulus has the same identity as the preceding one for example.
But in order to do so, we need to assign these features to the stimuli (for example with the table you suggested). And then we have to be able to compare the features of the stimuli with numeric values. For example: If identity of first image in the sequence == 1 then identity of second image ~= 1. And so forth.
So, this leads me back to my question: Do you know how to compare the features of the stimuli in the table with numeric values?
Best,
R
Jon
Jon el 26 de Oct. de 2020
Editada: Jon el 26 de Oct. de 2020
I've been away from the computer for awhile. Maybe you've already solved your problem. If not here are some further ideas.
If you have the images and their features in a table, let's call it T, you can draw a random row from the table using
sample = T(randi(numImages),:) % numSample is total number of images
note that sample is itself a table with just one row but you can access the various features using dot notation.
Suppose also that you had already saved your previous sample as lastSample (also a one row table)
So if your column names (variable names ) were imFile, id, con, gender, expression, age and you wanted to make sure that the sample was a different subject, and gender than the last one (just for example) you could then check
isGood = sample.id ~= lastSample.id && sample.gender~=lastSample.gender
if its good you can get the name of the image file and present it and then set your lastSample equal to your current sample
So I guess you would have a loop something like
% you will have to do some initialization here before entering loop
% ...
numPresented = 0
lastSample = T(randi(numImages),:) % random last sample just to initialize
% enter loop
while numPresented < numRequired
% draw a sample
sample = T(randi(numImages),:) % numSample is total number of images
% check criteria
% the ones below are just for illustration, you can set what you need in a similar way
isGood = sample.id ~= lastSample.id && sample.gender~=lastSample.gender
if isGood
image = sample.imFile; % and then show it to the user
% save the last sample for comparison
lastSample = sample
numPresented = numPresented + 1
end
end

Iniciar sesión para comentar.

Preguntada:

el 8 de Oct. de 2020

Editada:

Jon
el 26 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by