Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Assume that there is a matrix
m = magic(5)
ans =
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
As known, in order to change a specific values (eg: change to 0 if greater than 10) in a m×n matrix,
m(m>10) = 0
m =
0 0 1 8 0
0 5 7 0 0
4 6 0 0 0
10 0 0 0 3
0 0 0 2 9
I have a k×m×n matrix which consists of random 0s, 1s and 2s. k has iteration values 1 to 10 and will not be changed.
How can I change 1s to 0s then 2s to 1 sequentially? But kshould be unchanged. Only the values in m's and n's.
As an example of "Do it exactly the same way":
>> m = round(rand(3,2,2)*2)
m(:,:,1) =
1 1
0 0
1 2
m(:,:,2) =
1 1
0 1
2 1
>> m(m==1)=0
m(:,:,1) =
0 0
0 0
0 2
m(:,:,2) =
0 0
0 0
2 0
>> m(m==2)=1
m(:,:,1) =
0 0
0 0
0 1
m(:,:,2) =
0 0
0 0
1 0
The 3D logical mask returned by m==2 in this case can be used on any array with the same size.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an n x n matrix in MATLAB. I am trying to iterate through each row and column of this matrix. If the value in each element is higher than a certain threshold, I want to replace that element with a 1. If the value in each element is lower than a certain threshold, I want to replace that element with a 0.
I am trying to use two for loops, but it's not leading me anywhere.Any suggestions?
I suggest logical indexing.
A = randi([1 20],6,6);
Threshhold = 13;
A(A<Threshhold) = 0;
A(A>=Threshhold) = 1;
Before:
>> A = randi([1 20],6,6)
A =
7 1 20 3 2 15
16 13 11 3 11 7
5 2 1 5 10 16
5 14 8 14 11 8
16 11 7 20 20 17
10 1 2 10 6 12
After:
>> A
A =
1 0 0 0 1 0
0 0 1 0 0 0
0 0 0 1 0 0
1 1 1 0 1 1
0 0 0 1 0 0
0 0 0 0 0 0
Hope that helps.
Update:
Per #Cris Luengo from comments,
Other approaches include A=double(A>=Threshold) or equivalently A=+(A>=Threshold).
I need to transform a neural network output matrix with size 2 X N in zeros and ones, where 0 will represent the minimum value of the column and 1 contrariwise. This will be necessary in order to calculate the confusion matrix.
For example, consider this matrix 2 X 8:
2 33 4 5 6 7 8 9
1 44 5 4 7 5 2 1
I need to get this result:
1 0 0 1 0 1 1 1
0 1 1 0 1 0 0 0
How can I do this in MATLAB without for loops? Thanks in advance.
>> d = [ 2 33 4 5 6 7 8 9;
1 44 5 4 7 5 2 1];
>> bsxfun(#rdivide, bsxfun(#minus, d, min(d)), max(d) - min(d))
ans =
1 0 0 1 0 1 1 1
0 1 1 0 1 0 0 0
The bsxfun function is necessary to broadcast the minus and division operations to matrices of different dimensions (min and max have only 1 row each).
Other solution is the following (works only for 2 rows):
>> [d(1,:) > d(2,:); d(1,:) < d(2,:)]
ans =
1 0 0 1 0 1 1 1
0 1 1 0 1 0 0 0
If it's just 2xN, then this will work:
floor(A./[max(A); max(A)])
In general:
floor(A./repmat(max(A),size(A,1),1))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can i get the possible combinations to get 20 'i's from the given vector:
dn = 0 i i i i i i i i i i i i 0 i 0 i i i i i i i 0 i i i i 0 i i i i i i i i i 0 0 i i i i i i i 0 i i i 0 i i 0 i i i 0 i 0 0 0 0 0 0 i i 0 0 0 0 0 0 i i 0 0 0 0 i i 0 0 0 i 0 i 0 0 i 0 i 0 i i 0 i i 0
My objective
1. No of possible combinations with each combination having 20 i
2. Index value of each 'i's for all combination
Example:
var = 0 i i 0 i 0 i i 0 0 0 i
Here I need posiible combinations with 2 'i's
I can form the combinations like (2,3),(2,5),(3,5),(2,7) and so on.
I think this does what you want:
var = [0 i i 0 i 0 i i 0 0 0 i];
N = 2;
result = nchoosek(find(var==i), N);
In your example, this gives
result =
2 3
2 5
2 7
2 8
2 12
3 5
3 7
3 8
3 12
5 7
5 8
5 12
7 8
7 12
8 12
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have vector for example. Ones and zeros are representing traffic and idle states for base station traffic.
u=[1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ]
I know how to calculate number of ones and zeros in it after each break.
But I need help for delay. Zeros are telling me when I can put my base station in sleep mode.
Is there a way to make a delay for example that, base station does not go to sleep after first zero, instead of that that it goes to sleep after third zero, that means with some kind o delay.
Not a simple solution but yet I think you should find it interesting. You can optimize from here.
First of all I assumed you wanted to go to sleep after the third consecutive 0.
Check the example.
CODE:
u=[1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 ];
bw_u=bwlabel(u==0);
get_breaks=bsxfun(#eq, bw_u, unique(bw_u)');
pos_break=cumsum(get_breaks,2).*get_breaks;
third_0=pos_break(2:end,:)==3;
[~,indx_third_0]=find(third_0)
OUTPUT:
indx_third_0 =
8
20
32
43
Again assuming ASantosRibeiro interpreted your question correctly, you can do it a little more simply as follows. Let n denote the desired number of zeros. In your case, n=3;
>> ind = find(diff(conv(2*u-1, repmat(-1, [1 n])) == n) == 1) + 1
ind =
8 20 32 43
Assuming ASantosRibeiro interpreted your question correctly, you could do this to find when to "go to sleep":
delay = 3;
idx = find(diff(u)==-1) + delay
idx =
8 20 32 43
This is assuming you want a delay of 3.
diff(u) returns the difference between consecutive elements in u. Finding when the difference equals -1 is equivalent to finding the indices of when the light changes from 1 to 0. By adding the delay, you have your desired indices.
I want to calculate the average slope or gradient at each iteration in such a matrix.
a=[ 10 9 8 7 6 5 5;
9 9 8 7 8 5 5;
8 8 7 7 5 5 5;
7 7 7 6 5 5 5;
6 6 6.6 5 5 5 5;
6 6 6.1 5 5 5 5;
6.3 5 5 5 5 5 5]
Where I am wanting to find the slope or gradient between the a(1,1) position during each step and at each point that boarders a value of 5. Each iteration the position of the 5's changes and so do the other values.
After doing so I will then average the slope. I haven't encountered a problem like this yet and I could not find a Matlab command to simplify.
First you must find out which the coast elements are. From your definition, an element is a coast element if it border (from the right) with a 5. If the sea level is 5, and is the lowest possible value i.e. no element goes beyond sea level, then you must first find all the land elements as,
land=a>5;
This returns,
ans =
1 1 1 1 1 0 0
1 1 1 1 1 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 0 0 0 0
1 1 1 0 0 0 0
1 0 0 0 0 0 0
Now, the coast elements are 1s that are followed by a 0. Take the column difference of the land matrix,
coastTmp=diff(land,1,2);
returning,
ans =
0 0 0 0 -1 0
0 0 0 0 -1 0
0 0 0 -1 0 0
0 0 0 -1 0 0
0 0 -1 0 0 0
0 0 -1 0 0 0
-1 0 0 0 0 0
and find the -1s,
coast=find(coastTmp==-1);
which are,
coast =
7
19
20
24
25
29
30
From here it is easy. The gradient is the difference of a(1,1) with all the coast elements, i.e.
slope=a(coast)-a(1,1); % negative slope here
giving,
slope =
-3.700000000000000
-3.400000000000000
-3.900000000000000
-3.000000000000000
-4.000000000000000
-4.000000000000000
-2.000000000000000
and of course the mean is,
mean(slope);