matrix multiply even and odd by different value
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi, given a matrix zj
NL=4;NV=6;xiz=0.011e-3;f=1;
zj=repmat((1:NL),NV,1)
Let i be the rows and j be the column elements. So I would like to multiply each element of zj by the following condition
j*xiz % when i is odd
a.*((-1)+2.*j)+(-1).*((-1)+f+j).*(2.*a+xiz) % when i is even
For clarification I've attached an image of this in maths form. Thanks,
댓글 수: 0
채택된 답변
per isakson
2014년 12월 17일
편집: per isakson
2014년 12월 17일
Try this script
NL=4;NV=6;xiz=0.011e-3;f=1;
zj = repmat((1:NL),NV,1);
a = pi;
[C,R] = meshgrid( 1:size(zj,2), 1:size(zj,1) );
is_odd = isOdd( R );
is_even = not( is_odd );
out = nan( size(zj) );
out(is_odd) = zj(is_odd).* C(is_odd) .* xiz;
out(is_even)= zj(is_even).* a.*((-1)+2.*C(is_even))+(-1) ...
.*((-1)+f+C(is_even)).*(2.*a+xiz);
where
function iso = isOdd( val )
% cred: Subject: Even/odd, From: us, Date: 23 Aug, 2007 15:21:31
iso = logical( bitand( abs( val ), 1 ) );
end
 
Comment
I tried to implement the description in your question, which doesn't fully agree with the "image" - I think.
"multiply each element of zj by the following condition"   I read multiply by factor.
However, my script should be a starting point.
The function, isOdd, throws an error if the input is not a whole number (and it is fast).
>> isOdd( pi )
Error using bitand
Double inputs must have integer values in the range of ASSUMEDTYPE.
Error in isOdd (line 11)
iso = logical( bitand( abs( val ), 1 ) );
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!