Hello, hope someone can help me because I am totally blank!
I have a 10 by 6 matrix. Each row reflects the response of one participant and each column represents a different stimulus
responses = rand(10,6)
I also have 6 by 2 cell array. Each rows here represents each of the six stimulus and the column the condition they belong to
stimuli = {'a', 'HF'; 'c', 'MF'; 'g', 'MF'; 's', 'LF'; 'T', 'HF'; 'v', 'LF'}
I would like to classify the data into responses to HF, MF and LF conditions. I know that I would need to use a for loop and probably either if or switch statement, but however I am blank and don't even know how to start.
Thanks!

댓글 수: 6

Guillaume
Guillaume 2018년 3월 6일
I know that I would need to use a for loop and probably either if or switch statement
Most likely, it can be done a lot easier without loops, ifs or switches. However, I have no idea how the two arrays are linked other than the columns of one somehow correspond to the rows of the other.
Can you explain better what sort the data into responses mean?
Hi Guillaume, thanks for your reply. I mean to classify the data into responses to the different categories. I have now edited this.
Image Analyst
Image Analyst 2018년 3월 6일
And it's still confusing. Let's say that row 3, column 3 of the (poorly named) "a" is 0.4356. What do I do with that? Am I supposed to somehow pick one of the 12 letters in (the also poorly named) "b" to create your output/classification matrix? If so, which one? Please give an actual example with actual numbers and explain how each number was used to create the element in the output/classification matrix. http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
Alejandro Estudillo
Alejandro Estudillo 2018년 3월 6일
편집: Alejandro Estudillo 2018년 3월 6일
Sorry if I am not being clear enough. I am new in matlab and I find it difficult to explain this. I have rename the cell array (now stimuli) and the matrix (now responses), to try to make my point clearer. Let me start with the cell array stimuli. The first column represents the name of the actual stimulus (they are just letters). The second column represents the category that each stimulus belongs to. For example, the stimulus 'a' belong to the category 'HF' (this stands for high frequency), stimulus 'c' would be middle frequency (MF), etc.
Regarding the matrix each row represents an observations, while each columns correspond with each stimulus. In this sense, the first column represents stimulus 'a', the second one stimulus 'c' and so on . As shown in the cell array stimuli, each of these stimulus has a corresponding category. So what I want is to classify each column of the matrix responses into their corresponding category. For example, column 1 and 5 of the matrix responses corresponds to the category 'HF'. So I would like to store these two columns in a new variable (for example, high_frequency. Please, let me know if this makes sense now.
Thanks!
Guillaume
Guillaume 2018년 3월 6일
So, the first column of stimuli is irrelevant for your question, then? And really all you want to do is split responses into three matrices with columns [1 5] together, columns [2 3] together and columns [2 6] together (according to this particular stimuli cell array)
This can be easily done but the more important question is why? Splitting one variable into multiple variable usually makes subsequent calculations harder rather than easier. Keeping the labels of the columns separated as you have now is probably best.
Alejandro Estudillo
Alejandro Estudillo 2018년 3월 7일
편집: Alejandro Estudillo 2018년 3월 7일
Yes it is basically that. Columns 1 and 5 (both HF) would be in one new matrix, columns 2 and 3 (both MF) in other matrix and columns 4 and 6 (both LF) in a different one. I could assign directly the respective columns to the new matrices, but my actual matrix response is much more complex, with a lot of conditions, so I would need something to make the assignation automatically. I also need to make different calculations for each condition.
Thanks for all your help

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2018년 3월 7일

1 개 추천

As I said in my comments, it is very likely that splitting your matrix is going to make further processing more complicated, not easier. With that caveat:
[catnames, ~, subs] = unique(stimuli(:, 2)); %get unique names and corresponding rows/columns
splitresponses = accumarray(subs, (1:numel(subs))', [], @(cols) {responses(:, cols)});
splitresponses will be a cell array, where splitresponses{i} corresponds to all the responses for catnames{i}. Whatever you do, do not create individual variables for these.
If need be, you can convert the cell array into a structure whose fields are named after the stimuli:
responsesbystimuli = cell2struct(splitresponses, catnames)
but that again is likely to make your processing more complicated.

댓글 수: 2

I modified the code to my particular example and it works! Still trying to understand it though. Thanks!
To make it easier to understand, this is what the accumarray portion is more or less equivalent to:
splitresponses = cell(max(subs), 1);
for iter = 1:max(subs)
cols = find(iter == subs);
splitresponses{iter} = responses(:, cols);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 3월 6일

댓글:

2018년 3월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by