How to Automatically Shorten an Array so the number of Rows/Columns is divisible by 3

조회 수: 18 (최근 30일)
Hi Guys,
when reshaping an Vector into a Matrix with 3 rows, I naturally have to make sure the number of Elements in the Vector is divisible by 3, otherwise Matlab sends back an Error. So far I've always done this manually, simply by omitting the first couple of elements, until the overal number gets to something evenly divisible by 3
a (:,howevermanyelementsIgottadelete) = [];
As these Vectors usually have 10000+ Elements, it doesn't matter if the first couple of 'em aren't included.
Is there a way to have Matlab do this automatically, e.g. to automatically shorten a Vector until it's # of Elements ( or a Matrix until its # of Columns, or Rows) is evenly divisble by 3 ?
Kindest Regards,
Tim

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 5월 27일
you can just calculate how many vectors you need to delete.
somevector = rand(10000,1);
s = 3;
shortvector = somevector((end-s*floor(end/s)+1):end);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by