matrix multiply even and odd by different value

2 次查看(过去 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,

采纳的回答

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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by