How to add two values to a matrix each five values of that matrix in order to have a new matrix?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Andrea Di Lelio
el 14 de Sept. de 2019
Editada: Bruno Luong
el 14 de Sept. de 2019
Hello everyone,
I have one matrix "A" with dimension 358x1 and I am trying to form a new matrix "B" with dimension 520x1. I need to form the matrix "B" using with the values of "A" where every five values I would like the fifth to be duplicated and attached to itself two times.
More easily, supposing a matrix "XY" with dimension 15x1, I would like to duplicate the rows 5, 10 and 15 two times and then attach that values to themselves. At the end, I should have a new matrix "XYZ" with dimension 21x1. Practically:
XY = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15]
XYZ = [1; 2; 3; 4; 5; 5; 5; 6; 7; 8; 9; 10; 10; 10; 11; 12; 13; 14; 15; 15; 15]
Thank you in advance
0 comentarios
Respuesta aceptada
Jackson Burns
el 14 de Sept. de 2019
Hi Andrea!
Here's a function I wrote to achieve the goal:
function out = dup5(inputmatrix)
h=size(inputmatrix,1);
count = 1; out = zeros(h+floor(h/5)*2,1);
for i = 1:size(inputmatrix,1)
if mod(i,5)==0
out(count:count+2) = [inputmatrix(i); inputmatrix(i); inputmatrix(i)];
count = count + 3;
else
out(count) = inputmatrix(i);
count = count + 1;
end
end
0 comentarios
Más respuestas (2)
Bruno Luong
el 14 de Sept. de 2019
Editada: Bruno Luong
el 14 de Sept. de 2019
XY = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15]:
r = ones(size(XY));
r(5:5:end) = 3;
XYZ = repelem(XY,r)
Note if you expand (358 x 1) array you'll get (500 x 1) array.
0 comentarios
madhan ravi
el 14 de Sept. de 2019
ix=arrayfun(@(x)[x,x*(ones(~mod(x,5),2))],1:numel(XY),'un',0);
% multiple of 5----^ ^-- n times
idx=cat(2,ix{:});
XYZ = XY(idx,:)
0 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!