Filter löschen
Filter löschen

Re-arranging array columns in alternating form

1 Ansicht (letzte 30 Tage)
Saeid
Saeid am 18 Dez. 2018
Kommentiert: Saeid am 18 Dez. 2018
I would like to write a function:
function [Y]=AlternateColumns(X,n)
that takes an array X of the form:
AlterBefore.png
and turns it into Y of the form:
AlterAfter.png
In this example n=3, but it could be any number by which the entire number of columns is divisible. How is this possible without using a loop?

Akzeptierte Antwort

Guillaume
Guillaume am 18 Dez. 2018
Bearbeitet: Guillaume am 18 Dez. 2018
function Y = AlternateColumns(X, n)
Y = reshape(permute(reshape(X, size(X, 1), [], n), [1 3 2]), size(X, 1), []);
end
Basically, reshape into a 3D array, swap dimensions 2 and 3 and reshape back into original shape.

Weitere Antworten (0)

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by