In a specific range, finding the first value over a specific value and maximum value
이전 댓글 표시
I have a data set more. [A]
A= [1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0];
By using A matrix, two 1s in each range can seperate each range.
B= [1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6];
[1 2 3 4 5/ 1 2 3 4 5 6/ 1 2 3 4 5/ 1 2 3 4 5 6/ 1 2 3 4 5 6];
% two ranges 1~6 and 1~5
C= [0 1 2 8 2 0 0 1 7 1 2 0 1 1 9 4 0 0 1 5 2 1 0 1 1 2 6 1]
In each range, how to call correspoding B(1, i) of the first value over 0.5 and maximum value in C matrix.
댓글 수: 2
Guillaume
2015년 11월 18일
Please, show what output you want as I don't understand your question, nor how you define a range.
Note that in your question you've put some extra spacing between some numbers. Those extra spaces do not do anything, they are lost as soon as you enter the array in matlab.
I have no idea what the '/' symbol are supposed to mean. While what you've written is valid matlab syntax the effect of the '/' is simply to divide one number by the next so
[4 5/ 1 2 3] == [4 (5/1) 2 3] == [4 5 2 3]
Hyowon Lee
2015년 11월 18일
답변 (2개)
Thorsten
2015년 11월 18일
It's not clear to me what you want. I've you want to enter vectors of different length and find the first value and the maximum, you use cells and cellfun like
A = {[1 2 3 4 5], [10 2 3 4 5 6], [100 2 3 4 5]}
Afirst = cellfun(@(x) x(1), A)
Amax = cellfun(@max, A)
Guillaume
2015년 11월 18일
With these sort of setup, what you want to do is break up your vector into smaller vectors corresponding to the individual sequences. These individual vectors can either be stored as rows of a single matrix (using reshape) if they're all the same length, or in a cell array (using mat2cell) otherwise.
The difficulty is thus finding the length of each vector. In your case, since you have your A vector it's fairly easy, it's the difference of indices when A goes from 0 to 1. Note the second 1 in your A vector is unnecessary:
A = [1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0];
vectorlengths = diff(find(diff([0 A 1]) == 1))
You can then use these lengths to split your vector into a cell array, and apply whichever algorithm on each cell of the cell array:
C = [0 1 2 8 2 0 0 1 7 1 2 0 1 1 9 4 0 0 1 5 2 1 0 1 1 2 6 1];
splitC = mat2cell(C, 1, vectorlengths);
%to get the index of the 1st element above half maximum in each subvector:
D = cellfun(@(v) find(v > 0.5*max(v), 1), splitC)
I'll note that in your example that index is actually the maximum since there's no other value in each range above 0.5*maximum.
카테고리
도움말 센터 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!