matrix multiply even and odd by different value

2 ビュー (過去 30 日間)
Joseph
Joseph 2014 年 12 月 17 日
コメント済み: Joseph 2014 年 12 月 22 日
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,

採用された回答

per isakson
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
&nbsp
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" &nbsp 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 ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by