Write a function called even_indices that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument

Problem 3. Write a function called even_indices that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. The elements of the n-by-m output matrix are all zeros except for the ones for which both indices are even: these need to be ones. For example, here is an example run:even_indices(5,6) ans = 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0

2 Comments

Show what you've written to try to solve the problem and describe exactly where you're having trouble and you may receive some guidance.
Hello Steven Lord.. i want to create a code that generates the above matrices, but my problem i can not understand the trick behind the exact code. i tried to do so but i could not manage and this my job..
function z=even_indices(A,B) z= randi([0,1],[A,B]); end .. after run.. this is the result, even_indices(5,6)
ans =
1 0 0 0 1 1
1 0 1 0 0 1
0 1 1 1 1 0
1 1 0 1 1 1
1 1 1 1 1 0
Thank alot for your response, Mr Steven Lord

Sign in to comment.

 Accepted Answer

m = 5 ; n = 5 ;
A = zeros(m,n) ;
% even positions replace with 1
for i = 1:m
for j = 1:n
if ~mod(i,2) && ~mod(j,2)
A(i,j) = 1 ;
end
end
end

2 Comments

And in simple....
m = 5; n = 5 ;
A = zeros(m,n) ;
A(2:2:m,2:2:n) = 1 ;
thank you alot Mr KSSV. my pleasures to you for ever.. actually iam beginer in the computer programing and i always face some problems .. thank you again again .. God bless you..

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!