I have a vector called time, which contains time values. I'd like obtain a vector of indexes of time in which the value is between threshold x and threshold y.
This is undoubltedly trivial to do, but I'm struggling with Matlab syntax a bit, here. Any help would be greatly appreciated.
Blz
time=5:20
idx = find(time > 10 & time < 15) % indices
values=time(time(:)>10 & time(:)<15) % values
which give
time =
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
idx =
7 8 9 10
values =
11 12 13 14
Related
Can any one help or giving algorithm to find the number of steps from any number n to the center of a spiral matrix:
Spiral matrix could be of any odd size, like this one of size 5
17 16 15 14 13
18 5 4 3 12
19 6 1 2 11
20 7 8 9 10
21 22 23 24 25
How to find number of steps from any number like from 13 to the center 1? In In this example, from 13 to 1, the number of steps (going only left and right down up) is 4.
This will do:
% given
n = 13;
A = spiral(5);
[ii,jj] = ind2sub(size(A),find(A == n)); % x,y index of n
[c] = ceil(size(A)/2); % index of center
dis = abs(c(1) - ii) + abs(c(2) - jj); % distance
I am analyzing the efficiency of multiple circuits using matlab. The output is a maximum working distance and the power of the circuit analyzed. The output of this vectors is the following:
>> display(distance)
distance =
8 21 21 4 3 8 3 8 2 6 10 7 6 8 12 11 6 8
>>display(power)
power =
Columns 1 through 13
3.2047 3.5666 3.7578 1.8184 3.0810 3.7973 2.8699 3.3953 2.5971 3.1933 3.8191 3.7992 3.4802
Columns 14 through 18
4.1104 4.0836 3.2191 3.9155 0.2394
As you can see I have the circuit 1 that has a power of 3.2047 and a maximum distance of 8m. I have the circuit 6 that has the same maximum distance with a 3.7973 power. I want to rearrange the distance vector in order to it to be crescent (starting with 2 and ending with 21 for instance) and be able to rearrange the power accordingly. Below you have the display that I would like to see doing this to clarify this question.
>> display(distanceReorganized)
distanceReorganized =
2 3 3 4 6 6 6 7 8 8 8 8 8 10 11 12 21 21
>>display(powerReorganized)
powerReorganized =
Columns 1 through 13
2.5971 2.8699 3.0810 1.8184 3.1933 3.4802 3.9155 3.7992 0.2394 3.2047 3.3953 3.7973 4.1104
Columns 14 through 18
3.8191 3.2191 4.0836 3.5666 3.7578
Use the second output argument from sort.
[~,I] = sort(distance, 'ascend');
distanceReorganized = distance(I);
powerReorganized = power(I);
Edit: Based on your comment, sortrows should do what you are looking for:
circuitData = table(distance', power', 'VariableNames', {'Distance', 'Power'});
sortrows(circuitData,{'Distance','Power'})
You can then reassign the columns of the table to new vector variables should you wish to.
Here is the documentation for sortrows.
I have a 20*120 matrix. For each column in the matrix I need to find the maximum value between all the values, and then sum the remaining values. Then I need to divide the maximum value by the summation of the remaining values. I tried the following code but the result was not correct. What is the problem?
s = 1:z %z=120
for i = 1:x %x=20
maximss = max(Pres_W); %maximum value
InterFss = (sum(Pres_W))-maximss; %remaining values
SIRk(:,s) = (maximss(:,s))./(InterFss(:,s));
end
Instead of answering "what's wrong", I'll first provide a solution explaining how this should be done:
Say we have an example matrix m as follows:
m =
8 5 9 14 10 7 5
10 8 12 11 9 9 12
10 3 7 7 8 4 6
13 11 6 15 13 11 9
Find the maximum value of each column:
col_max = max(m, [], 1)
col_max =
13 11 12 15 13 11 12
Sum all elements in each column, and substract the maximum values:
col_sum = sum(m, 1) - col_max
col_sum =
28 16 22 32 27 20 20
Divide the maximum value by the sum of the other elements:
col_max ./ col_sum
ans =
0.46429 0.68750 0.54545 0.46875 0.48148 0.55000 0.60000
Or, as a one-liner:
max(m,[],1)./(sum(m,1)-max(m,[],1))
ans =
0.46429 0.68750 0.54545 0.46875 0.48148 0.55000 0.60000
By the way: Your code does exactly what you're explaining, it returns the maximum value divided by all values except the maximum value.
Notes regarding best practice:
Vectorize things like this, no need for loops.
max(m, [], 1) is the same as max(m) for 2D-arrays. However, if your matrix for some reason only have one row, it will return the maximum value of the row, thus a single number.
sum(m,1) is the same as sum(m) for 2D-arrays. However, if your matrix for some reason only have one row, it will return the sum of the row, thus a single number.
The input is an N-by-1 matrix. I need to reshape it to L-by-M matrix. The following is an example.
Input:
b =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Set length = 18, Output:
X =
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
8 9 10
9 10 11
10 11 12
11 12 13
12 13 14
13 14 15
14 15 16
15 16 17
16 17 18
17 18 19
18 19 20
Because I have a very big matrix, using a loop to reshape is very inefficient. How can I improve the reshape speed?
Your example output matrix X is the perfect matrix to index a vector of length N to get what you want. It's also very easy to create using bsxfun:
N = 20;
b = rand(N,1);
M = 3; %// number of columns
L = N-M; %// Note that N-M is an upper limit for L!
idx = bsxfun(#plus, (0:L)', 1:M)
X = b(idx)
That's exactly what im2col (from the Image Processing Toolbox) does:
b = (1:20).'; %'// example data
L = 18; % // desired length of sliding blocks
x = im2col(b, [L 1]); % // result
I'd use horzcat. For example:
function X = reshaper(b,len)
diff = length(b) - len + 1;
X = b(1:len);
for i=2:diff
X = horzcat(X,b(i:len+(i-1)));
end
You could probably remove the for loop with some further thought.
I have a list of coordinates I would like to sample from a Matrix.
Is there any elegant way to do it?
Ideally, something that looks like:
A = magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
r = 1:5; % row coordinates
c = 5:-1:1; % column coordinates
A(r,c)
ans =
15 14 13 12 11
Which is equivalent to
for k=1:length(r)
A(r(k), c(k))
end
I am sure someone has asked that, but I couldn't find it anywhere.
Applying #excaza comment I was able to solve this with:
rc_ids = sub2ind(size(A), r,c);
A(rc_ids)