Index are not used completely

Hello, me again. I have a set of data. I have a to calculate small current. So I need max and min. But since is a noisy signal I can't use peak2peak. So what I did. In a range predetermine. I take the max.. find the index. later from that index I calculate a new range, to make the mean around that number. For each cell I have 5 traces. My problem is the following: I calculate the max for each trace, the index, then when I want to calculate the mean, the range it choose to find the new numbers is not taken in consideration. Only uses the first index I calculate.
[valmax, idxmax]= max(input.current(:,1002:1032),[],2);
indexFromZero (:,ii,jj)= 1002+idxmax-1;
MaximumMean(:,ii,jj)= mean(input.current(:,indexFromZero-1:indexFromZero+1),2);
indexMinimumRange(:,ii,jj)=indexFromZero (:,ii) +40;
[valmin, idxmin]= min(input.current(:,indexMinimumRange-1:indexMinimumRange+1),[],2);
MinmumMean(:,ii,jj)=mean(input.current(:,indexMinimumRange-1:indexMinimumRange+1),2);
Ih(:,ii)=MaximumMean(:,ii)-MinmumMean(:,ii);
So indexFromZero is a matrix with the index for all the traces.But it always calculates in the range of 1017, the first number.
1017
1016
1026
1024
1021
[ii and jj are the cell number and the sheet from excel where the information of the cell is]

Réponses (1)

Guillaume
Guillaume le 27 Juin 2017
Modifié(e) : Guillaume le 27 Juin 2017

0 votes

It's not clear in your code what is ii and jj. All your other variable names are very descriptive, why not these two? It's also not clear why sometimes you use both ii and jj as index, and sometimes just ii (meaning jj default to 1)
Anyway, as per the documentation of colon (the : operator), when given non-scalars, it only uses the first element: "If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1)".
The simplest way to solve your problem is to loop over the elements of indexFromZero. If you want to avoid looping, you'll have to do some gymnastics with sub2ind to create indexing matrices.
edit: just spotted that you do explain what ii and jj are. Why not call them, cellnumber and sheetnumber then? A lot more obvious as to their purpose. Still don't understand why you have:
indexFromZero(:,ii,jj) = something; %indexFromZero is 3D
indexMinimumRange(:,ii,jj) = indexFromZero(:,ii) + 40; %now it's 2D, what happened to jj?

9 commentaires

Milagros ARIETTI
Milagros ARIETTI le 27 Juin 2017
Thank you for the answer. Like I said, [ii and jj are the cell number and the sheet from excel where the information of the cell is]. I have CFS file, that I have excel with the name of the cell, so matlab can take the right file. The ii is the cell, and the jj is the sheet where the cell is. Different sheets is different experiment.
Guillaume
Guillaume le 27 Juin 2017
Yes I'd edited my answer while you were typing your comment. In any case, the ii and jj is incidental to your problem which I've also answered.
Although, it does look like there is also a bug with the ii and jj as commented in my edit.
Milagros ARIETTI
Milagros ARIETTI le 27 Juin 2017
indexFromZero doesn't change even if jj or ii are there, I use them for the big loop, when it changes to the next experiment. My problem is that is not using the entire index, only the first number in indexFromZero , and not the others. I guess I can't avoid using a loop for each trace, each cell and each sheet.
Guillaume
Guillaume le 27 Juin 2017
Modifié(e) : Guillaume le 27 Juin 2017
Forget about ii and jj, there is a bug there but it has no impact on your direct problem. The problem, as said, is that you can't pass a vector to :. It only uses the first element of the vector. You can loop over the elements of indexFromZero or try:
%not using ii and jj since it's bugged. Irrelevant to the problem at hand
indexFromZero = 1002 + idxmax - 1;
MaximumMean = mean(input.current(sub2ind(size(input.current, ...
repmat((1:size(input.current, 1))', 1, 3), ...
indexFromZero + [-1, 0, 1])), 2);
indexMinimumRange = indexFromZero + 40;
[~, idxmin] = min(input.current(sub2ind(size(input.current, ...
repmat((1:size(input.current, 1))', 1, 3), ...
indexMinimumRange + [-1, 0, 1])), ...
[], 2);
MinmumMean = mean(input.current(sub2ind(size(input.current, ...
repmat((1:size(input.current, 1))', 1, 3), ...
indexMinimumRange + [-1, 0, 1])), 2);
Ih = MaximumMean - MinmumMean;
Completely untested. Plus, I've got a feeling that the calculation of your mispelt minmumMean should be based on idxmin not indexMinimumRange as you wrote.
thank you again, it has a problem with the + in this line. But I am trying to do other code, for other things... I am really bad at this, although I read books, did a course. I am sorry.
if true
MaximumMean = mean(input.current(sub2ind(size(input.current, ...
repmat((1:size(input.current, 1))', 1, 3), ...
indexFromZero + [-1, 0, 1])), 2);
You must be using an old version < R2016b, replace all the
xxx + [-1, 0, 1]
by
bsxfun(@plus, xxx, [-1, 0, 1])
Milagros ARIETTI
Milagros ARIETTI le 28 Juin 2017
Modifié(e) : Stephen23 le 28 Juin 2017
I am sorry I tried, but can't make it work,
[valmax, idxmax]= max(input.current(:,1004:1031),[],2);
indexFromZero(:,ii,jj)=1004+idxmax-1;
MaximumMean = mean(input.current(sub2ind(size(input.current,repmat((1:size(input.current, 1))', 1, 3)), bsxfun(@plus, indexFromZero, [-1, 0, 1]));
says there is ( missing, and I really don't understand have of the thing, so I don't know where is missing. I tried at the end but then the size function gives an error that is too large.
Stephen23
Stephen23 le 28 Juin 2017
Modifié(e) : Stephen23 le 28 Juin 2017
Calling repmat on the size used inside sub2ind is very strange:
sub2ind(size(input.current,repmat((1:size(input.current, 1))', 1, 3))
Perhaps you really intended to simply do this:
sub2ind(size(input.current), 1, 3)
which in the complete line (with fixed missing parenthesis):
MaximumMean = mean(input.current(sub2ind(size(input.current), 1, 3),bsxfun(@plus, indexFromZero, [-1, 0, 1])));
Even better would be to split that line up and use a few temporary variables (at least during the writing and testing phases).
@Stephen, of course repmat on the first argument to sub2ind is absurb. That's because I was missing a closing bracket. The repmat is on the 2nd argument.
@Milagros, as said it is untested code. The line should be:
MaximumMean = mean(input.current(sub2ind(size(input.current), ...
repmat((1:size(input.current, 1))', 1, 3), ...
bsxfun(@plus, indexFromZero, [-1, 0, 1]))), 2);
still untested...
If you don't understand what the code is doing or how it is working even with the help of the documentation then you may be better off explicitly looping over the elements of indexFromZero.

Connectez-vous pour commenter.

Catégories

Commenté :

le 28 Juin 2017

Community Treasure Hunt

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

Start Hunting!

Translated by