1
votes

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.

image

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.

1
It sounds like you are referring to an image...? But also, are you trying to do something like this: M = 10*ones(5); M([1,4:end], [2]) = -16? - Dan
"As you can see"—actually, I can't see anything since you probably forgot to include the image. Also, I recommend MATLAB's excellent documentation, which you can access with the doc command. There are great tutorials on matrices and arrays. - dasdingonesin
Thank you for the quick response. See the image in the following link: i.stack.imgur.com/s9DXy.png - André
Can you show your current approach with the loop? - Matthias W.
for i = 182; j= 1019:x_end; z(i,j)=-16; end where x_end equals 1183. I have repeated this loop for 128 times.. which is not very efficient. - André

1 Answers

2
votes

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.