Index are not used completely
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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]
0 comentarios
Respuestas (1)
Guillaume
el 27 de Jun. de 2017
Editada: Guillaume
el 27 de Jun. de 2017
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 comentarios
Stephen23
el 28 de Jun. de 2017
Editada: Stephen23
el 28 de Jun. de 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).
Guillaume
el 28 de Jun. de 2017
@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.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!