Is there a one-line code for this?

1 visualizzazione (ultimi 30 giorni)
Mitsu
Mitsu il 27 Giu 2021
Commentato: Mitsu il 27 Giu 2021
% I would like to go from A = [1 1;2 2;3 3;4 4;5 5]
% to A = [1 1;2 2;3 3;4 4;5 5;1 2;2 3;3 4;4 5;5 6];
A = [1 1;2 2;3 3;4 4;5 5];
% Multi-line approach
B = A;
B(:,2) = B(:,2) + 1;
A = [A;B];
clear B
% Is there a way to do this in one line of code? I have to do similar operations multiple times,
% and would like to know if there is a way to do so. Otherwise, I will settle for a function.
% Note that the actual matrices are 1,000,000+ x 30 in size.
% Thank you

Risposta accettata

Stephen23
Stephen23 il 27 Giu 2021
A = [1 1;2 2;3 3;4 4;5 5];
A = [A;A+(0:1)]
A = 10×2
1 1 2 2 3 3 4 4 5 5 1 2 2 3 3 4 4 5 5 6
  3 Commenti
Stephen23
Stephen23 il 27 Giu 2021
"the simplest way is the following then?"
Probably the "simplest way" is just as you show in your question.
If you want to avoid duplicated data, you could create B first then add one to the appropriate (part-)column.
Mitsu
Mitsu il 27 Giu 2021
Understood. Thank you.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by