If I have two sequences A and B containing the elements a1, a2, ... and b1,b2, ... where a_i and b_i are of dimension 1xn and 1xm, respectively, then I want to make a new sequence C which contains: a_i(1)*b_i, a_i(2)*b_i, ... , a_i(n)*b_i. So for the ith element of C, I want to have the Kronecker product of the elements a_i and b_i. I want to code this in Matlab, but without a for-loop. For the case when b_i are scalars, the desired result is achieved with
C = A.*B
However, this does not work for non-scalar b_i's. So what I do now is for sequences of length L:
C = [];
for ii = 1:L
C = [C; kron(A(ii,:),B(ii,:))];
end
But I have the idea that this must be possible without a for loop. Here is my code using the symbolic toolbox:
clc; clear;
L = 5;
syms('a1',[L,1]);
syms('a2',[L,1]);
syms('b1',[L,1]);
syms('b2',[L,1]);
A = [a1,a2];
B = [b1,b2];
C1 = A.*B % only for size(B,2)=1
C2 = [];
for ii = 1:L
C2 = [C2;kron(A(ii,:),B(ii,:))];
end
C2
C3 = kron(A,B) % does not work
Here reshape and implicit expansion are used to compute the result:
C = reshape(B .* reshape (A, L, 1, []), L, []);
Related
I have multiple variables, which are 2x2 arrays. I would like to stored all these into one cell array is there a for loop that could be used for this
I do not recommend the following solutions because of the reasons summarised here: https://au.mathworks.com/help/matlab/matlab_prog/string-evaluation.html.
I'll leave it to you to decide whether it's worth using eval or avoiding it.
(1. Option) If you have A, B, and C representing your 2x2 arrays, then:
A = rand(2);
B = rand(2);
C = rand(2);
array_names = ["A", "B", "C"];
D = cell(1, numel(array_names));
for ii = 1:numel(D)
D{ii} = eval(array_names(ii));
end
Should you have more than 3 arrays, just extend array_names accordingly.
(2. Option) If you have A1, A2, and A3 representing your 2x2 arrays, then:
A1 = rand(2);
A2 = rand(2);
A3 = rand(2);
D = cell(1, 3);
for ii = 1:numel(D)
D{ii} = eval("A"+ii);
end
Should you have more than 3 arrays, just change the size of the allocated cell D accordingly.
I have a 3d array A, e.g. A=rand(N,N,K).
I need an array B s.t.
B(n,m) = norm(A(:,:,n)*A(:,:,m)' - A(:,:,m)*A(:,:,n)','fro')^2 for all indices n,m in 1:K.
Here's the looping code:
B = zeros(K,K);
for n=1:K
for m=1:K
B(n,m) = norm(A(:,:,n)*A(:,:,m)' - A(:,:,m)*A(:,:,n)','fro')^2;
end
end
I don't want to loop through 1:K.
I can create an array An_x_mt of size NK x NK s.t.
An_x_mt equals A(:,:,n)*A(:,:,m)' for all n,m in 1:K by
An_x_mt = Ar*Ac_t;
with
Ac_t=reshape(permute(A,[2 1 3]),size(A,1),[]);
Ar=Ac_t';
How do I create an array Am_x_nt also of size NK x NK s.t.
Am_x_nt equals A(:,:,m)*A(:,:,n)' for all n,m in 1:K
so that I could do
B = An_x_mt - Am_x_nt
B = reshape(B,N,N,[]);
B = reshape(squeeze(sum(sum(B.^2,1),2)),K,K);
Thx
For those who can't/won't use mmx and want to stick to pure Matlab code, here's how you could do it. mat2cell and cell2mat functions are your friends:
[N,~,nmat]=size(A);
Atc = reshape(permute(A,[2 1 3]),N,[]); % A', N x N*nmat
Ar = Atc'; % A, N*nmat x N
Anmt_2d = Ar*Atc; % An*Am'
Anmt_2d_cell = mat2cell(Anmt_2d,N*ones(nmat,1),N*ones(nmat,1));
Amnt_2d_cell = Anmt_2d_cell'; % ONLY products transposed, NOT their factors
Amnt_2d = cell2mat(Amnt_2d_cell); % Am*An'
Anm = Anmt_2d - Amnt_2d;
Anm = Anm.^2;
Anm_cell = mat2cell(Anm,N*ones(nmat,1),N*ones(nmat,1));
d = cellfun(#(c) sum(c(:)), Anm_cell); % squared Frobenius norm of each product; nmat x nmat
Alternatively, after computing Anmt_2d_cell and Amnt_2d_cell, you could convert them to 3d with the 3rd dimension encoding the (n,m) and (m,n) indices and then do the rest of the computations in 3d. You would need the permn() utility from here https://www.mathworks.com/matlabcentral/fileexchange/7147-permn-v-n-k
Anmt_3d = cat(3,Anmt_2d_cell);
Amnt_3d = cat(3,Amnt_2d_cell);
Anm_3d = Anmt_3d - Amnt_3d;
Anm_3d = Anm_3d.^2;
Anm = squeeze(sum(sum(Anm_3d,1),2));
d = zeros(nmat,nmat);
nm=permn(1:nmat, 2); % all permutations (n,m) with repeat, by-row order
d(sub2ind([nmat,nmat],nm(:,1),nm(:,2))) = Anm;
For some reason, the 2nd option (3D arrays) is twice faster.
Hopes this helps.
In my code I have a slow part of which the idea can be summarized in the following short example:
A = randi(10,5); %Random 5×5 matrix containing integers ranging from 0 to 10
B = rand(10,1); %Random 10×1 vector containing values ranging from 0 to 1
C = B(A); %New 5×5 matrix consisting of elements from B, indexed using A
In my case, the matrix A is sized 1000×1000, B is a 500×1 vector and C is also 1000×1000. Given that this 3rd line is in a for loop, where A is constant and B is updated every iteration, how can I further improve speed performance? According to the profile viewer 75% of code execution is at this single line. As expected, using a for loop for this operation is much slower (10x for a 1000×1000 matrix):
AA = A(:); %Convert matrix to vector
for k=1:length(AA) %Loop through this vector and use it as index
D(k) = B(AA(k));
end
E = reshape(D,5,5); %Reshape vector to matrix of 5x5
Any ideas to optimize this?
Edit: Script used to measure performance:
N = 1500;
A = randi(500,N);
AA = A(:);
D = zeros(N,N);
B = rand(500,1);
f1 = #() VectorIndex(A,B);
timeit(f1,1)
f2 = #() LoopIndex(AA,B,N);
timeit(f2,1)
function C = VectorIndex(A,B)
C = B(A);
end
function D = LoopIndex(AA,B,N)
D = zeros(N,N);
for k=1:length(AA)
D(k) = B(AA(k));
end
D = reshape(D,N,N);
end
I have a matrix A=[1,2,3] and a cell B={[1,2,3],[1,2,5],[1,2,6]}.
I would like the product of the matrix elements of a similar cell
that's mean A * B ={1*[1,2,3],2*[1,2,5],3*[1,2,6]};
Without loop in matlab
Here a few ways to multiply as you described:
% inputs
A = [1,2,3];
B = {[1,2,3],[1,2,5],[1,2,6]};
using for-loop:
C1 = cell(size(B));
for i=1:numel(C1)
C1{i} = A(i) * B{i};
end
using cellfun:
C2 = cellfun(#(a,b)a*b, num2cell(A), B, 'Uniform',false);
using bsxfun:
C3 = bsxfun(#times, A(:), cat(1,B{:}));
C3 = num2cell(C3,2)';
All results should be equal (output being a cell array):
assert(isequal(C1,C2,C3))
If I were to choose, I would stick with the for-loop. In this case it's likely faster and easiest to read.
I have a series of arrays of equal length, and want to make a matrix for each data point of these, and perform some sort of operation such a multiplying the matrices.
a=ones(1,10);
b=3*ones(1,10);
c=zeros(1,10);
for i=1:10
A(i)=[a(i) a(i);
b(i) b(i)];
B(i)=[c(i) c(i)];
C(i)=B(i)*A(i);
end
Is this possible without using cells?
A = zeros(2,2,length(a));
B = zeros(length(a),:);
C = zeros(size(B));
for i=1:10
A(:,:,i)=[a(i) a(i);
b(i) b(i)];
B(i,:)=[c(i) c(i)];
C(i,:)=B(i,:)*A(:,:,i);
end
Note you can make A and B without loops:
aa = permute(A, [3,2,1]);
bb = permute(B, [3,2,1]);
A = [aa,aa;bb,bb];
B = [c.', c.'];