Hi, i need to change all the values in the index where i,j are both even or odd to zero and multiple by 2 all the other. for example:
change
1,2,3
4,5,6
7,8,9
to
0,4,0
8,0,12
0,16,0

댓글 수: 1

James Tursa
James Tursa 2015년 4월 28일
편집: James Tursa 2015년 4월 28일
Sounds like homework. Are you instructed to use any particular method (for loop, logical indexing, etc)?

댓글을 달려면 로그인하십시오.

답변 (4개)

Stephen23
Stephen23 2015년 4월 30일
편집: Stephen23 2015년 4월 30일

0 개 추천

You might find toeplitz useful for this:
>> n = 4;
>> toeplitz(mod(1:n,2))
ans =
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
You could use MATLAB's rather powerful indexing ability to solve this problem, in which case you can convert that matrix of ones and zeros to a logical array using logical.
Andrei Bobrov
Andrei Bobrov 2015년 4월 30일

0 개 추천

variant:
a = [1,2,3
4,5,6
7,8,9];
[i1,i2] = ndgrid(1:size(a,1));
out = rem(i1+i2,2)*2.*a;
Thorsten
Thorsten 2015년 4월 30일

0 개 추천

index = 1:9;
ieven = mod(index, 2) ==0;
index(ieven) = 2*index
index(~ieven) = 0

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

질문:

2015년 4월 28일

답변:

2015년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by