Clear Filters
Clear Filters

how can I take elements of an array as input to a function in a set of 10 elements each?

1 view (last 30 days)
I have an array of [1x676].
I need to input this array [1x676] in a function through testbench. how can I input it in a set of 26 elements each.?
this is part of a code that is needed for hdlcoder. so I have to reduce number of IOs

Answers (3)

John D'Errico
John D'Errico on 21 Jun 2023
Edited: John D'Errico on 21 Jun 2023
A terribly confusing question. (Even if I ignore the fact that your subject says 10 elements, but the question text says 26 elements each.) Do you merely want to pass in blocks of 26 elements, one block at a time into the function?
If so, then just use a loop. People get too hung up on needing to have everything neatly vectorized, so it works in one line as if by magic. At the same time, that one line becomes impossible to read, debug and maintain, and runs no more quickly than a simple loop. And this would not be even a big loop.
If you really insist on something that makes even the above loop more easy, then first reshape the array, into a 26x26 array.
help reshape
RESHAPE Reshape array. RESHAPE(X,M,N) or RESHAPE(X,[M,N]) returns the M-by-N matrix whose elements are taken columnwise from X. An error results if X does not have M*N elements. RESHAPE(X,M,N,P,...) or RESHAPE(X,[M,N,P,...]) returns an N-D array with the same elements as X but reshaped to have the size M-by-N-by-P-by-.... The product of the specified dimensions, M*N*P*..., must be the same as NUMEL(X). RESHAPE(X,...,[],...) calculates the length of the dimension represented by [], such that the product of the dimensions equals NUMEL(X). The value of NUMEL(X) must be evenly divisible by the product of the specified dimensions. You can use only one occurrence of []. See also SQUEEZE, SHIFTDIM, COLON. Documentation for reshape doc reshape Other uses of reshape categorical/reshape quaternion/reshape codistributed/reshape RandStream/reshape datetime/reshape se2/reshape digraph/reshape se3/reshape dlarray/reshape so2/reshape duration/reshape so3/reshape gpuArray/reshape sym/reshape graph/reshape symbolic/reshape InputOutputModel/reshape tabular/reshape lib.pointer/reshape tall/reshape matlab.mixin.indexing.RedefinesParen/reshape
Then, taking each column of that array as a vector, pass it into your function, one at a time. Seriously, there is nothing sophisticated needed here.
Or, do you want some magical piece of code that will generate all possible subsets of 26 elements? If so, then you need to wait for a new computer, possibly waiting many years, since the number of ways you can extract subsets of size 26 from 676 elements is literally too large to even comprehend.
nchoosek(676,26)
Warning: Result may not be exact. Coefficient has a maximum relative error of 8.8818e-15, corresponding to absolute error 5.128895226573073e+32.
ans = 5.7746e+46
So, roughly the square of Avogadro's number. HUGE. IMMENSE.
Or maybe you are wanting somethign else. Perhaps some code that will choose an optimal subset from that large set. In that case, you need to explain what it is you really need to do.
I'm betting that you just need to use a simple loop.

Mudit Kumar Bhugari
Mudit Kumar Bhugari on 21 Jun 2023
As per my knowledge, you have an array of size [1x676] and you want to input it into a function in sets of 26 elements each. You can divide the array into smaller subsets of size 26 and pass them to the function one by one.
Here's a sample code which can give you a better understanding
arr = 1:676;
% Divide the array into subsets of size 26
setSize = 26;
numSets = numel(array) / setSize;
for i = 1:numSets
stIdx = (i - 1) * setSize + 1;
endIdx = stIndex + setSize - 1;
subset = array(startIndex:endIdx);
% Call your function with the current subset as input
output = function_name(subset);
end

Lakshay Rose
Lakshay Rose on 21 Jun 2023
Hi Jyoti Nautiyal,
As per my understanding you are trying to divide your array of 1X676 into sets of 26 each and then input them to a function.
To get the following result you can try the below code –
array = 1:676; % Example array [1x676]
numSets = ceil(length(array) / 26); % Calculate the number of sets
for i = 1:numSets
startIndex = (i - 1) * 26 + 1; % Calculate the start index of the current set
endIndex = min(startIndex + 26 - 1, length(array)); % Calculate the end index of the current set
currentSet = array(startIndex:endIndex); % Extract the current set of 26 elements
% Call your function with the current set as an input
yourFunction(currentSet);
end
You can replace the `yourFunction` with the function which you want to call.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!