Cody

Problem 498. Find the Kronecker Tensor Product without using KRON

Solution 65038

Submitted on 23 Mar 2012 by Aurelien Queffurust
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
% test for 'KRON' usage fid = fopen(which('mykron'),'r'); c=onCleanup(@()fclose(fid)); tline=fgetl(fid); while ischar(tline) if (strfind(tline,'=kron')>0), error('Don''t use kron'); end if (strfind(tline,' kron')>0), error('Don''t use kron'); end if (strfind(tline,'+kron')>0)|(strfind(tline,'*kron')>0), error('Don''t use kron'); end if (strfind(tline,'-kron')>0), error('Don''t use kron'); end tline = fgetl(fid) end;

tline = [ma,na] = size(A); tline = [mb,nb] = size(B); tline = '' tline = '' tline = '' tline = % At least one input is sparse, result is sparse. tline = '' tline = [ia,ja,sa] = find(A); tline = [ib,jb,sb] = find(B); tline = ia = ia(:); ja = ja(:); sa = sa(:); tline = ib = ib(:); jb = jb(:); sb = sb(:); tline = ka = ones(size(sa)); tline = kb = ones(size(sb)); tline = t = mb*(ia-1)'; tline = ik = t(kb,:)+ib(:,ka); tline = t = nb*(ja-1)'; tline = jk = t(kb,:)+jb(:,ka); tline = K = sparse(ik,jk,sb*sa.',ma*mb,na*nb); tline = '' tline = '' tline = '' tline = '' tline = %This code written by profile_id 2391181 tline = -1

2   Pass
%% test 1 x =1:3; y =2:4; y_correct = [2 3 4 4 6 8 6 9 12]; assert(isequal(mykron(x,y),y_correct))

3   Pass
%% test 2 x = randi(10,3,3); y = 2:4; z_correct = kron(x,y); assert(isequal(mykron(x,y),z_correct))

Suggested Problems

More from this Author16

Community Treasure Hunt

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

Start Hunting!