184 views (last 30 days)

how to fill a matrix of size 180 * 180 by results obtained by another program knowing that I will go through the matrix in a way 4 * 4 example

The first box must be filled by 3 values of 1 and a 0 by the following four boxes by 2 values of 1 and two values of 0, the following four values a value of 1 and a value of 0, and so on. at the end of the matrix 180 * 180

note: the number of values of 1 and 0 is the output of another program and it is the program entry that I am developing it

Resulat=[1,0,1,1,0.0,.........

1,1,0,0.0.1,..........]

Image Analyst
on 28 Dec 2018

Well you could use blockproc() to march along the data one element at a time and replacing the output block with the number of zeros specified in the input block in random locations. This requires the Image Processing Toolbox. Essentially it's

outputMatrix = blockproc(outputMatrix,[windowSize windowSize], myFilterHandle)

A full demo is attached.

A =

4 2 1 4 1

4 4 4 2 4

1 3 3 4 2

1 4 4 4 2

outputMatrix =

1 1 1 1 0 0 1 1 0 0

1 1 0 0 0 1 1 1 0 1

1 1 1 1 1 1 0 1 1 1

1 1 1 1 1 1 1 0 1 1

1 0 1 1 1 1 1 1 1 1

0 0 1 0 0 1 1 1 0 0

0 0 1 1 1 1 1 1 0 1

1 0 1 1 1 1 1 1 1 0

Note how each 2x2 block of the output array has as many 1's in it as the corresponding number in the input array A.

Image Analyst
on 28 Dec 2018

It already IS saved in a variable called outputMatrix.

If you want to store it to a file on disk that another program can read in, you can use save():

fileName = fullfile(pwd, 'outputMatrix.mat')

save(fileName, 'outputMatrix');

then, in the other program use load():

s = load(fileName);

outputMatrix = s.outputMatrix;

Sign in to comment.

Omer Yasin Birey
on 28 Dec 2018

Edited: Omer Yasin Birey
on 28 Dec 2018

Hi dahkli, try this

result = eye(180);

for i = 1:numel(result)

temp = rand>(0.25*mod(i,3));

result(i) = temp;

end

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_654951

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_654951

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_654955

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_654955

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_655062

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_655062

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_655129

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/437473-how-to-filling-a-matrix#comment_655129

Sign in to comment.