matrix multiply even and odd by different value
    5 views (last 30 days)
  
       Show older comments
    
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 Comments
Accepted Answer
  per isakson
      
      
 on 17 Dec 2014
        
      Edited: per isakson
      
      
 on 17 Dec 2014
  
      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 ) );
More Answers (0)
See Also
Categories
				Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
