Sending/calling every row of matrix to another function from current function

2 views (last 30 days)
Hi guys !
I have built a function that gives me a matrix that its size is mXn , this function is:
function TruncatedSubstring=Truncated(input1,substring) % this function gives me a matrix of all substrings occurances at each row
N = 16;
positions = strfind(input1(1:end-N+1), substring);
TruncatedSubstring= cell2mat(arrayfun(@(idx) input1(idx+length(substring):idx+length(substring)+N-1), positions, 'uniform', 0 ).');
end
I want to take every row that's found in the output of my above function, in other words every row of the matrix TruncatedSubstring and send it to another function (found in another script), the other function called function DoSomething=Dosomething(substring) and it's found in another script..so I want to call that function in the function TruncatedSubstring with given input every row of the matrix TruncatedSubstring.
function DoSomething=Dosomething(substring)
%function that does and manipulate some operations on given substring-in other words in my case in my current matrix's row at every call to that function
the function DoSomething does some operations/manipulations on the given input called substring.
substring is an input to function Dosomething that's implicitly equal to every row of matrix output of the called function(I call the function Dosomething with every row output matrix as input to that function)
To clarify more, I will example a more concerete example:
lets assume the output of function TruncatedSubstring is a matrix size 2X50, this means that the rows I 2 and at every row I have implicitly substring .. so I want to call at every row of the output TruncatedSubstring the function Dosomething(current matrix row vector) with current row of the matrix output of function TruncatedSubstring, so in my case here I call twice the function Dosomething, and first call (call to function Dosomething)with input the first matrix row vector, the second call with input the second matrix row vector.....till I finish the row output vectors(rows of output matrix of TruncatedSubstring function)
How can I implement that in matlab? thanks alot !

Accepted Answer

hosein Javan
hosein Javan on 12 Aug 2020
[N,~] = size(TruncatedSubstring); % N = number of rows
for i=1:N
substring = TruncatedSubstring(i,:)
Result(i,:) = Dosomething(substring)
end
  5 Comments
Steven Lord
Steven Lord on 12 Aug 2020
If all you want is the number of rows, you don't need to call size with two ouputs and ignore the second. You can tell size explicitly that you want the number of rows.
A = zeros(3, 4, 5);
numRows = size(A, 1) % size in the first dimension
Explicitly specifying a dimension can also avoid a problem when the array has more dimensions than you think it does. The first call below does the right thing (as described in the documentation) in returning 20, but that's probably not what you'd expect. The second call does the right thing, both by the documentation and by your expectation.
[~, numColsWrong] = size(A) % No, NOT a bug!
numColsRight = size(A, 2)
hosein Javan
hosein Javan on 12 Aug 2020
Steven Lord. it worked all the time for me, I haven't yet encounterd unexpexted results, even in the case of more dimensions that you mentioned.
A = ones(100,500,200);
tic % for large arrays not faster
[r1,~] = size(A) % the matrix is 3d while I only reffered to two dimensions
toc
tic
r2 = size(A,1)
toc

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!