Resizing an array - removing elements

I have an array of data, in this case 61 rows long. I need to reshape this into 3s - LSB1 = reshape(LSB,3,[]); but as 61 isn't divisible by 3, it wont work.
I want to be able to remove the last value of from the vector in this case, via an if statement (sometimes maybe to remove the last 2 values if there were 62 rows instead) i.e. an if statement to check divisibility by 3 and then to remove the correct number of elements from the end of the array.
Thank you

1 comentario

Ahmad Kanzu Syauqi Firdaus
Ahmad Kanzu Syauqi Firdaus el 3 de Sept. de 2018
you can also use imresize, especially when you want to decrease array dimension without changing array content generally. example:
X=rand(105,1); M=61; Xnew=imresize(X,[M,1]);

Iniciar sesión para comentar.

 Respuesta aceptada

Star Strider
Star Strider el 6 de En. de 2016
If you only want 60 rows in your array, just redefine a new array to have 60 rows:
LSB = rand(61,1); % Create Data
LSBnew = LSB(1:60,:);
LSB1 = reshape(LSBnew, [], 3);

6 comentarios

S
S el 6 de En. de 2016
Thank you!
Star Strider
Star Strider el 6 de En. de 2016
My pleasure!
S
S el 8 de En. de 2016
Hi again, How can I reshape a vector down so it is divisible by 3? Some of my data has 57 elements and therefore need to be brought down to 56 in this case. Is there a way to automate this for any data input?
Thanks!
My pleasure!
I don’t understand the 57 and 56 issue, though, since 57/3=19, and 56/3=18.666...
Yes. To do that, my code changes to:
LSB = rand(1, 56); % Create Vector
vec_len = length(LSB) - rem(length(LSB),3); % Trim To Have Length = Integer Multiple Of 3
LSB_new = reshape(LSB(1:vec_len), [], 3); % Reshape To Matrix
S
S el 8 de En. de 2016
Editada: S el 8 de En. de 2016
Thanks again!
One last thing, is there a way to reshape the vector so that elements 1,2 & 3 go across the new matrix (and then 3,4,5 etc) rather than going down the matrix?
so for a vector from 1-9, the reshaped one would read:
[1 2 3, 4 5 6, 7 8 9] rather than
[1 4 7, 2 5 8, 3 6 9]
My pleasure!
Since you are starting with a column vector, reverse the last two arguments in reshape and transpose the result:
x = [1:9]';
y = reshape(x, 3, [])';
This should do what you want.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 6 de En. de 2016
Editada: Stephen23 el 6 de En. de 2016
Here is an example showing how this can be done automatically:
>> X = rand(4,3); % 4*3 = 12, not divisible by 5
>> R = 5;
>> reshape(X(1:end-mod(numel(X),R)),R,[])
ans =
0.79221 0.93399
0.95949 0.67874
0.65574 0.75774
0.035712 0.74313
0.84913 0.39223

Categorías

Productos

Etiquetas

Preguntada:

S
S
el 6 de En. de 2016

Comentada:

el 3 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by