Cell entries as the third dimension of the cell2mat matrix.

1 Ansicht (letzte 30 Tage)
I am using this code to initialize A and B:
for k=1:N
for j=1:N
A{k,j}=S(:,j).*D(k,:).';
B(k,j,:)=A{k,j};
end
end
Essentially, I want to create B directly from A without explicit loops as I found a vectorized way to create A.
  2 Kommentare
Rik
Rik am 20 Dez. 2022
You can probably use reshape and repmat to get implicit expansion to do this for you. What have you tried?
Also, you don't need A at all here to get B. Do you need A later?
Anish Pradhan
Anish Pradhan am 20 Dez. 2022
I do need A later. I have tried:
C = cellfun(@transpose,A,'un',0);
B2= reshape(cell2mat(C),[N,N,N])
But B and B2 are different in arrangement. For context, they both are part of an elaborate gradient calculation.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 20 Dez. 2022
Compute B outside of the loop
for k=1:N
for j=1:N
A{k,j}=S(:,j).*D(k,:).';
end
end
B = permute(reshape(horzcat(A{:}),size(A,1),size(A,2),[]),[2 3 1]);
Confirm that it's equal to the loop version of B
N = 3; % Demo data
S = 100 * rand(N);
D = 5 * rand(N);
for k=1:N
for j=1:N
A{k,j}=S(:,j).*D(k,:).';
BLoop(k,j,:)=A{k,j};
end
end
B = permute(reshape(horzcat(A{:}),size(A,1),size(A,2),[]),[2 3 1]);
isequal(BLoop, B) % True means BLoop and B are equal
ans = logical
1
Explanation
This is our goal
BLoop
BLoop =
BLoop(:,:,1) = 233.0973 127.4516 305.5177 202.6588 110.8086 265.6223 66.6101 36.4207 87.3051 BLoop(:,:,2) = 188.3584 54.7575 69.7297 325.8464 94.7265 120.6274 235.6702 68.5114 87.2444 BLoop(:,:,3) = 447.2431 180.8590 27.1600 279.9638 113.2135 17.0015 343.8104 139.0322 20.8788
First the columns of A are concatenated into a matrix
horzcat(A{:})
ans = 3×9
233.0973 202.6588 66.6101 127.4516 110.8086 36.4207 305.5177 265.6223 87.3051 188.3584 325.8464 235.6702 54.7575 94.7265 68.5114 69.7297 120.6274 87.2444 447.2431 279.9638 343.8104 180.8590 113.2135 139.0322 27.1600 17.0015 20.8788
Then the matrix is reshaped into a 3D array according to the shape of A. The shape looks like our goal but note that the order of the values do not match the goal.
reshape(horzcat(A{:}),size(A,1),size(A,2),[])
ans =
ans(:,:,1) = 233.0973 202.6588 66.6101 188.3584 325.8464 235.6702 447.2431 279.9638 343.8104 ans(:,:,2) = 127.4516 110.8086 36.4207 54.7575 94.7265 68.5114 180.8590 113.2135 139.0322 ans(:,:,3) = 305.5177 265.6223 87.3051 69.7297 120.6274 87.2444 27.1600 17.0015 20.8788
Finally, the dimensions of the 3D array are rearranged to move dim 1 to dim 3 per the instructions in "BLoop(k,j,:)"
B = permute(reshape(horzcat(A{:}),size(A,1),size(A,2),[]),[2 3 1])
B =
B(:,:,1) = 233.0973 127.4516 305.5177 202.6588 110.8086 265.6223 66.6101 36.4207 87.3051 B(:,:,2) = 188.3584 54.7575 69.7297 325.8464 94.7265 120.6274 235.6702 68.5114 87.2444 B(:,:,3) = 447.2431 180.8590 27.1600 279.9638 113.2135 17.0015 343.8104 139.0322 20.8788
Confirm that we have met the goal
isequal(B,BLoop)
ans = logical
1

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by