MATLAB: How to change value for linear increasing column-structure - matlab

My question is about changing values in a matrix linearly. I have a 594x1183 matrix and each cell has a value of 10. I want to change certain parts in a matrix to other values (see image below). In the solid-lined box I have a matrix with values of 10. In the dash-lined box I want to have a value of -16.
As you can see, from column 1019 to end (1183) the value should be -16. This also holds for column 1020 (to end) ... to column 1054 (to end) for the rows 54 to 182.
I can do it either manually with Excel (time-consuming) or make for every row a loop (128 loops, also time-consuming). I think there must be a quicker way to solve this problem.
So basically, for the first row (1), column 1019 to the end of matrix (column 1183) should have a value of -16 (in the first row column 1 to 1018 it has a value of 10 and from 1019 to 1183 it has a value of -16). Then the next row, the column 1020 to the end of matrix (1183) should have a value of -16 as well (in the second row, column 1 to 1019 it has a value of 10) .... repeating this to the column 1054 in row 128. So in the last row column 1 to 1053 it has a value of 10 and from 1054 to 1183 it has a value of -16.

You can make a coordinate system via meshgrid, and use that to make inequalities to use the logical indexing of arrays.
y = 594;
x=1183;
x0 = 1054;
x1 = 1019;
y0 = 54;
y1 = 182;
A = 10*ones(y,x);
[X,Y]=meshgrid(1:x,1:y);
A( Y >= y1*(X-x0)/(x1-x0) + y0*(x1-X)/(x1-x0) & Y <= y1 & Y >= y0 ) = -16;
You can check that with the spy(A) command.

Related

Calculate the average of elements in a matrix that correspond to a value in a separate matrix

I have a 333 x 333 adjacency matrix which consists of values that I would like to average according to the identity of each cell, which is defined in a separate 333x1 vector. There are a total of 13 different groups defined in the second vector, so ideally, I'd be able to calculate a new 13 x 13 matrix in which each cell contained the average value of the corresponding values from the larger matrix.
matrix_1: 333 x 333 --> contains values for each pairwise interaction
vector_2: 333 x 1 --> contains the identity (range: 1 - 13) for each of the elements in matrix_1 (elements are the same in both the rows and columns)
ideal output = matrix_2: 13 x 13 --> contains values in each cell which reflect the mean score for all examples of the specific identity comparison.
e.g. matrix_2(1,1) --> should contain mean score of all 1 to 1 values from matrix_1
e.g. matrix_2(1,2) --> should contain mean score of all 1 to 2 values (and 2 to 1 values) from matrix_1
Thanks in advance
Mac
I'm not 100% certain from your description, but I guess you want:
[I,J] = ndgrid(V);
out = accumarray([I(:),J(:)], M(:), [], #mean);

Set max and min values of a matrix

I have a matrix 640-by-480 where each element has data. I want to set the values of the element to zero where the original values are not between two numbers. For example, A is a 640-by-480 matrix. For the i-th element A(i), if the value of A(i) is between 10 and 20 leave it, if not then set A(i)=0. Could anyone suggest a simple way instead of using loops?
Try this for values between 10 and 20:
A(A < 10 | A > 20) = 0;
The expression "A < 10 | A > 20" creates a logical mask, then the values under this mask are set to zeros.

row 2 and row 1 difference

I have one column matrix with n rows of postitive and negative values . I want difference between row 2 and row 1 , row 3 and row 2 and so on. I should need the difference in a single column
How do you want to measure the difference?
You cannot get all those differences in one single column without using a suitable norm, try "help norm"
[n,m]= size(A);
d=zeros(1,n)
for k= 1:n-1
d(k) = norm(A(k,:))-norm(A(k+1,:));
end
The output is stored in the column-vector d.

Find the mean of two rows in of a 42 by 4 matrix

I want to find the mean of 1st and 22nd row, the 2nd and 23rd row and so on of a 42-by-4 matrix. The first and 22nd rows are:
0 0 -30 -2.49000000000000
0 0 -30 -2.38000000000000
How can I find the mean of each column in these two rows?
MATLAB has a special syntax for indexing matrices, and you can learn about that by typing
help :
Now, suppose your matrix is
M = randn(42,4); %generating a random matrix with 42 rows and 4 columns
Then you can compute the mean of the desired rows using a simple add and average:
rowmeans = ( M(1:21,:) + M(22:end,:) ) / 2;
which will produce a matrix containing 21 rows and 4 columns, where each row is the desired average.
More generally, for averaging the top half and bottom half of a matrix that has an even number of rows:
rowmeans = ( M(1:end/2,:) + M(end/2+1:end,:) ) / 2;
You might also want to learn about the end keyword in MATLAB:
help end
If you want the mean of each colum of the two rows you can use something like
mean(t([1,22],:));
this will result to
0 0 -30.0000 -2.4350

Indicator matrix in Matlab

In matlab, I have a double datatype variable named Label with dimension 1211 x 1.
I would like to create a IndicatorMatrix(6 columns) such that if a row in the Label variable is 34 then the corresponding row in the IndicatorMatrix should be 0 0 1 1 0 0.
I mean 1 # 3 and 4th column of Indicator matrix.
Let x be the 1211x1 matrix (Label), and let im (IndicatorMatrix) be the matrix you wish to create. Do:
h = size(x,1);
im = [zeros(h, 2), repmat(x == 34, 1, 2), zeros(h, 2)];
This creates a matrix which is a horizontal concatentation of a zero matrix with 2 columns and height h, then a boolean matrix of x == 34 (which has 1 where x was 34, and zero in other places) repeated 2 times horizontally and once vertically, and then again another zero matrix.
Note that in your case we could have replaces h by 1211, but I tried to write more generic code.