Help extracting data from 3D matrices

조회 수: 3 (최근 30일)
Giovanni Bambini
Giovanni Bambini 2023년 11월 8일
댓글: Voss 2023년 11월 9일
Given a matrix A, such that size(A) = {m,n,l}, and a vector v: mx1,
I would like to obtain a 2D matrix F: mxn, in which:
each row of F is given by [i, : , v(i)].
And I would like to do this parametrically, with 1 line. An example to play with below.
m = 2;
n = 2;
l = 3;
A = [];
A(:,:,1) = [1 0;
0 0];
A(:,:,2) = [ 0 2;
0 0];
A(:,:,3) = [0 0;
3 0];
v = [2;3];
% I would like to obtain something like below, but parametrically and
% without for/if/etc.
F = [];
F(1, :) = squeeze(A(1, : , v(1)));
F(2, :) = squeeze(A(2, : , v(2)));
A
A =
A(:,:,1) = 1 0 0 0 A(:,:,2) = 0 2 0 0 A(:,:,3) = 0 0 3 0
F
F = 2×2
0 2 3 0
Thanks everyone,
E.
  댓글 수: 4
Dyuman Joshi
Dyuman Joshi 2023년 11월 8일
"for loops and if logics break code execution and increase computation time considerably."
Do you have a source for this?
Yes, vectorization is faster than using loops, but that does not mean loops are slow.
Let's compare Voss's answer below to a for loop approach -
%Taking a big sample for proper testing
A = rand(2500,500,100);
v = randi(size(A,3),size(A,1),1);
fun1 = @() forloop(A,v);
fun2 = @() vectorization(A,v);
z1 = fun1();
z2 = fun2();
%Check for equality
isequal(z1,z2)
ans = logical
1
fprintf('Time taken by the for loop is %f secs', timeit(fun1))
Time taken by the for loop is 0.021966 secs
fprintf('Time taken by the vectorized method is %f secs', timeit(fun2))
Time taken by the vectorized method is 0.026751 secs
function F = forloop(A,v)
[m,n,l] = size(A);
F = zeros(m,n);
for k=1:m
F(k,:) = A(k,:,v(k));
end
end
function F = vectorization(A,v)
[m,n,l] = size(A);
idx = sub2ind([m,n,l],repelem(1:m,1,n),repmat(1:n,1,m),repelem(v(:).',1,n));
F = reshape(A(idx),[],m).';
end
As you can see from the above results, the for loop is the faster method here.
Though one can argue that there might be a better method utilizing vectorization, but the point I am trying to convey, is that vectorization being faster does not mean for loops are considerably slower (unless not used properly)
Giovanni Bambini
Giovanni Bambini 2023년 11월 9일
Your analysis is really interesting, but I don't know if it considers all possible scenarios. Discarding the fact that for() are way faster than IFs when implemented properly and thus the difference may not be noticible, cpu and computers are nowadays very complex structures (several level of caches, multicores and NoCs, different types of cores (i.e. Intel P,E), vectorized instructions and accelerators (AVX, etc.), Out of order execution, ...) and thus a simple code with a not-huge memory footprint, even if bigger than the original, may not incapsulate all scenarios.
Saying that, it is highly possible that in the above case, as well as in several other cases, for() loops may be faster than other implementations, but as a personal rule (and also for ease of reading the code) I prefer to use vectorized commands possibly already implemented by Matlab like the aforementioned sub2ind() and reshape(), because I assume they are already implemented in the best way possible and highly scalable.
Anyway your insight was really helpful, and in the future I will not restrain from for() loops as strictly as before.
E.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 11월 8일
Here's one way:
A = cat(3,[1 0; 0 0],[0 2; 0 0],[0 0; 3 0]);
v = [2;3];
[m,n,l] = size(A);
idx = sub2ind([m,n,l],repelem(1:m,1,n),repmat(1:n,1,m),repelem(v(:).',1,n));
F = reshape(A(idx),[],m).';
disp(F);
0 2 3 0
  댓글 수: 2
Giovanni Bambini
Giovanni Bambini 2023년 11월 9일
Thanks, this is the kind of magic I was looking for. Didn't know about the sub2ind function.
Voss
Voss 2023년 11월 9일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by