Data not indexing properly?

3 views (last 30 days)
Lindsay
Lindsay on 18 Aug 2014
Commented: Geoff Hayes on 19 Aug 2014
Hi there,
I am compiling group data and plotting it with a best fit line, but am having issues with the way some of the output is indexing. For instance, I pull in the group data in a loop and concatenate it so I have all my data in one place. Then, I create an index for the values greater than zero and try to index into that later to make a new variable for best fit line plotting. Everything works until I do the last step, where it just seems to index into the 1st set of data I imported.
for isubj = 1:length(SUBJECTS)
if isubj == 1
qthres = xlsread(xlsfile, 1,'J3:J16');
peakamp = xlsread(xlsfile, 1, 'L3:L16');
scalae = xlsread(xlsfile, 1, 'C3:C16'); % can only use elecs 2-15 here because of qthres!
[STi, STj, ST] = (find(scalae == 1 & peakamp > 0));
[SMi, SMj,SM] = (find(scalae == 2 & peakamp > 0));
[SVi, SVj,SV] = (find(scalae == 3 & peakamp > 0));
[peakampi, peakampj] = find(peakamp > 0);
all the way to 8...
else isubj = 8
qthres = xlsread(xlsfile, 8,'J3:J16');
peakamp = xlsread(xlsfile, 8, 'L3:L16');
scalae = xlsread(xlsfile, 8, 'C3:C16');
[STi, STj, ST] = (find(scalae == 1 & peakamp > 0));
[SMi, SMj,SM] = (find(scalae == 2 & peakamp > 0));
[SVi, SVj,SV] = (find(scalae == 3 & peakamp > 0));
[peakampi, peakampj] = find(peakamp > 0);
end
....plot data which works fine...
if ~isrow(peakamp(peakampi))
peakamp(peakampi) = (peakamp(peakampi))';
end
allpeakamp = cat(2, allpeakamp, peakamp);
allthres = cat(2, allthres, qthres);
allscalae = cat(2, allscalae, scalae);
[findampi, findampj] = find(allpeakamp > 0);
[findthresi, findthresj] = find(allthres > 0);
...and end loop.
Both inside and outside the loop the above variables allpeakamp and findampi (for instance) look correct. However, when I try to do allpeakamp(findampi) so I can get values only above zero, it only indexes into subject 1's amplitudes, when it should be looking as a whole. I know something is wonky with how I"m doing it, but I can't put my finger on it. Essentially I need to compile the group amplitudes, find what's above zero, and use that to plot a best fit line against threshold...and the same issue is occurring with threshold as well.
Sorry this is so long. Thanks!

Accepted Answer

Geoff Hayes
Geoff Hayes on 18 Aug 2014
Lindsay - your code after the plotting starts with the following if statement
if ~isrow(peakamp(peakampi))
peakamp(peakampi) = (peakamp(peakampi))';
end
The condition checks to see if the data in peakamp indexed at peakampi is not a row, and then tries to replace the data with the transpose of that data? There will be no change to the vector peakamp - it will still be a column. Is the intent to modify the column vector to a row vector, or is it something else? Why only consider some of the elements in the vector (i.e. those referenced by peakampi) when the next line
allpeakamp = cat(2, allpeakamp, peakamp);
uses all of the peakamp data?
-------------------
Given that
peakamp = xlsread(xlsfile, 1, 'L3:L16');
reads a number of rows from one column of the Excel spreadsheet, it is going to be a column vector of negative and/or positive values. Then
allpeakamp = cat(2, allpeakamp, peakamp);
is going to concatenate along the column dimension of allpeakamp the peakamp column vector. Over time, this becomes a 14x8 matrix.
Now when you do
[findampi, findampj] = find(allpeakamp > 0);
look at the results for findampi and findampj. Both are column vectors, the first corresponding to the row indices (so will be values from 1-14, repeated for each subject that has data greater than zero) and the second corresponding to the column indices (so will be values from 1-8, repeated for each subject that has data greater than zero). Both vectors will be of the same length.
Now when you evaluate
allpeakamp(findampi)
the code is only using the row indices and not using the column indices from findampj and so the result will only ever be repeated values from the first subject. Here is a small example
A = [1 2 ; 3 4; 5 6];
The first column has odd numbers, the second even. Now we will do something similar
[rowIdcs, colIdcs] = find(A>0)
rowIdcs =
1
2
3
1
2
3
colIdcs =
1
1
1
2
2
2
Clearly, all elements of A are positive, yet when we try
A(rowIdcs)
ans =
1
3
5
1
3
5
we only see the odd numbers from the first column. That is because when accessing a matrix with a single vector, there is an assumption that we are trying to access these elements using the linear index equivalents to the row and column subscripts. So the repetitive nature of peakampi means that we will only ever access elements from the first column of allpeakamp i.e. the first subject.
To get around this, convert the row and column subscripts of peakampi and peakampj respectively to their linear index equivalents using sub2ind as
linIdcs = sub2ind(size(allpeakamp),peakampi,peakampj);
allpeakamp(linIdcs) % to return all positive values across all subjects
Try replacing with the above two lines, and see what happens!
  8 Comments
Lindsay
Lindsay on 19 Aug 2014
It worked! And whoa is this code much more concise. T Hanks again -- I appreciate receiving an explanation so I can learn for the future rather than just someone throwing code at me. This has made my final analysis so much easier, Geoff!
Annnnnd my p-value is still significant ;)
Geoff Hayes
Geoff Hayes on 19 Aug 2014
Glad to have been able to help, Lindsay!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!