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)]) = [];
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 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)]
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
I've a DenseMatrix
1 2 3 0 0 0 0 0 0
0 0 0 11 22 33 0 0 0
0 0 0 0 0 0 111 222 333
I want to remove the first row and then a last row with all 0s
0 0 0 11 22 33 0 0 0
0 0 0 0 0 0 111 222 333
0 0 0 0 0 0 0 0 0
How do I achieve this in Breeze ?
First, gather the rows you still want:
val subset = matrix(::, 2 to 3)
then add the zeroes:
val newMatrix = DenseMatrix.horzcat(subset, DenseMatrix.zeros[Double](1,9))
I might have mixed up rows and columns in the last line.
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.
I could not figure out the last part of my research so if anyone could help me I would be really appreciated for the help.. :)
Say that my original matrix is,
X =
0 0 0 0 0
0 0 12 9 0
0 4 9 15 0
0 11 19 0 0
0 2 4 8 0
0 4 5 8 0
0 0 0 0 0
and after finding the average of the non-zeros I will get something like below:
new_x =
0 0 0 0 0
0 0 **9.0000** 9.0000 0
0 4.0000 9.0000 **9.0000** 0
0 **8.3333** **8.0000** 0 0
0 2.0000 4.0000 8.0000 0
0 4.0000 5.0000 8.0000 0
0 0 0 0 0
Note that any elements that are greater than 10 are the 'center' and we want to find the average of the non-zeros with the radius of say 1 m. where 1 meter = 1 element away from the center.
** ** means the center.
For this part I have used the following (from gnovice):
X=[0 0 0 0 0; 0 0 12 9 0; 0 4 9 15 0; 0 11 19 0 0;
0 2 4 8 0; 0 4 5 8 0; 0 0 0 0 0];
kernel=[0 1 0; 1 0 1; 0 1 0];
sumx=conv2(X,kernel,'same');
nx=conv2(double(X>0),kernel,'same');
index=(X>10);
new_x=X;
new_x(index)=sumx(index)./max(nx(index),1);
So my question is that I want to compare the neighbor elements with its center whether they are equal, lesser, or greater. If it is greater or equal then '1' or else '0'.Also whatever elements that are outside the radius can be ignored and replaced with '0'.
For example, the 9 in the middle is within the radius of 12, 15, and 19 centers, so take the minimum center of those `min[9.000, 9.000, 8.000] = 8.000.
In this case 4 will not take into the consideration as it is not called the 'center' as well as [ 8 4 5 and 8 ] in the last two rows.
So I want something like below:
Test_x =
0 0 0 0 0
0 0 1 1 0
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
I have put this first part in the forum before and I am really appreciated for every suggestion earlier.
Please give me some ideas to start with. I have tried using a loop but it didnt seem to work very well. Any MATLAB function that can do the job for me..
Thank you so much for the help.
Beginner at MATLAB
I think I found the solution for this question by using Jonas techniques. Thank you for the help Jonas and gnovie:)
X=[0 0 0 0 0; 0 0 12 9 0; 0 4 9 15 0; 0 11 19 0 0; 0 2 4 8 0; 0 4 5 8 0; 0 0 0 0 0];
kernel=[0 1 0; 1 0 1; 0 1 0];
sumx=conv2(X,kernel,'same');
nx=conv2(double(X>0),kernel,'same');
avg_x=X;
avg_x(avg_x<10)=0;
index=(avg_x>10);
avg_x(index)=sumx(index)./max(nx(index),1);
avg_x =
0 0 0 0 0
0 0 9.0000 0 0
0 0 0 9.0000 0
0 8.3333 8.0000 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
tmp_x=avg_x;
maxVal=max(avg_x(:))+1;
tmp_x(tmp_x==0)=maxVal;
tmp_x=imerode(tmp_x,kernel);
Test_x=X>=tmp_x;
I think what you want to do is create a new array based on new_x that replaces every element by the minimum of its 4-connected neighbours. Then you can compare the new array to new_x.
Here's a way to do this (requires the image processing toolbox)
tmp_x = new_x;
maxVal = max(new_x(:))+1;
tmp_x(tmp_x == 0) = maxVal; %# replace all the zeros in tmp_x with something large
tmp_x = imerode(tmp_x,kernel); %# kernel is the same as in the OP
Test_x = new_x >= tmp_x; %# put ones wherever the value is
%# greater or equal the neighbour's minimum
%# only keep 1's that are next to 'centers'
Test_x = Test_x .* imdilate(X>10,strel('disk',1))
Test_x =
0 0 0 0 0
0 0 1 1 0
0 0 1 1 0
0 1 1 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
Note that I get one more ones with this logic than you do.