how to get the posiible combinations using matlab [closed] - matlab

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

Related

Trim( remove zeroes at the boundaries ) RGB Matrix in MatLab [closed]

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 5 years ago.
Improve this question
I have a RGB matrix like this:
0 0 0 0 0 0 0 0
0 0 23 0 0 0 0 0
0 1 255 0 0 130 22 0
0 49 0 0 0 0 120 0
0 0 0 0 79 0 213 0
0 0 0 0 0 0 0 0
Need to trim(remove zeroes at the boundaries) this matrix to be like:
0 23 0 0 0 0
1 255 0 0 130 22
49 0 0 0 0 120
0 0 0 79 0 213
You can using sum to find rows and columns which are all zeros. Then, remove them.
s1 = find(sum(mat,2)>0);
s2 = find(sum(mat,1)>0);
mat([1:(s1(1)-1) s1(end)+1:size(mat,1)], :) = [];
mat(:, [1:(s2(1)-1) s2(end)+1:size(mat,2)]) = [];

Shifting specific elements in a logical array [closed]

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 6 years ago.
Improve this question
So I have this matrix:
0 1 0
1 0 0
0 0 0
0 0 1
0 0 1
0 1 0
0 1 1
I don't know what statement(s) I have to write to change the bottom row only so that it becomes:
0 1 0
1 0 0
0 0 0
0 0 1
0 0 1
0 1 0
1 0 1
Specifically, I'd like to swap the 1st and 2nd column of of the 7th row only.
Not only that but I'd like to write a statement that finds adjacent 1's value in a row such as 1 1 0 or 0 0 0 1 1 0 and then applies the same switching.
Suppose that the variable mat represents your matrix. use:
mat(7,[1,2]) = mat(7,[2,1]);
Where 7 specifies the row which you want to perform the swapping on, and 1 and 2 are the columns to swap.

How to create an array according to row and column number [duplicate]

This question already has answers here:
How can I change the values of multiple points in a matrix?
(3 answers)
Closed 6 years ago.
I want to create an array according to row number and column number.
For example :
row_number Column_number Value
1 1 5
3 2 10
4 6 4
7 5 66
The array should look like :
A=
5 0 0 0 0 0
0 0 0 0 0 0
0 10 0 0 0 0
0 0 0 0 0 4
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 66 0
Otherwise it will print zero.
3 possible methods to this regularly asked question:
1. Using the sub2ind function
A = zeros(max(row_number), max(Column_number));
idx = sub2ind(size(A),row_number, Column_number);
A(idx) = Value;
2. Calculating the linear indices manually
A = zeros(max(row_number), max(Column_number));
idx = row_number(:,1) + (Column_number(:,2)-1)*size(A,1)
A(idx) = Value;
3. Use a sparse matrix
sparse(row_number, Column_number, Value)
And then call full on that if you want to convert it to a regular matrix

Random generate unique matrix [closed]

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 7 years ago.
Improve this question
Could you help me. I want to generate matrix of '0' and '1' (for example rows 8 and columns 7; r and c). I want to specify fixed size columns (in this example fixed=3) and on it position in every execution should be all '0'. The rest should be randomly and unique selected. Here is example:
0 0 0 1 0 1 0
0 0 0 0 0 0 1
0 0 0 1 0 0 0
0 0 0 1 1 0 0
0 0 0 0 0 1 1
0 0 0 1 0 1 0
0 0 0 1 1 1 1
0 0 0 0 1 1 1
my_matrix = [zeros(7, 3), floor(rand(7,4)*2)]

How to change value in 3d matrix in matlab? [closed]

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.