필터 지우기
필터 지우기

Outputting function into multiple columns of matrix

조회 수: 2 (최근 30일)
michael hosseini
michael hosseini 2019년 7월 16일
댓글: michael hosseini 2019년 7월 17일
I have a existing matrix M: r x 14 x d double
I have a function that takes in matrix: r x 6 x d double and it apparently outputs the same shape back.
The function manually names each column to a variable (say a1 - a6) , then outputs them in format [A1, A2, ..., A6]
I give it: M(:,[1:4 8:9],:), which is r x 6 x d, and I get back from function ans = r x 6 x d
However, when I am trying to re-insert the ans back into the same columns they came from (M(:,[1:4 8:9],:)), I get an error:
"Subscripted assignment dimension mismatch."
This is what I am trying:
M(:,[1:4 8:9],:) % outputs shape r x 6 x d double
M(:,[1:4 8:9],:) = MyFun(M(:,[1:4 8:9],:) )
function [A1, A2, A3, A4, A5, A6]= MyFun(vals,:) )
A1 = vals(:,1,:);
A2 = vals(:,2,:);
% And so on... Shape of each is 584 x 1 x 4
[A1, A2, A3, A4, A5, A6] % outputs shape r x 6 x d double
end
Is there a way to do this in one line similar to the above? Or is it just silly mistake etc.
Many thanks for any help

채택된 답변

Shashank Sharma
Shashank Sharma 2019년 7월 17일
편집: Shashank Sharma 2019년 7월 17일
Instead of returning [A1, A2, A3, A4, A5, A6] seperatly redifine your function to return AA.
Where, AA = [A1 A2 A3 A4 A5 A6];
when assigning to a variable from a function which returns an array of values, variables are assigned to in order of the returned values.
For eg,
function [x, y] = func %a function
z = func; %assign return value of func to z
In this case z contains only the value of x.
This is the reason for your error.
If you are not allowed to change your function, you can do the following,
[A1, A2, A3, A4, A5, A6] = MyFun(M(:,[1:4 8:9],:) );
M(:,[1:4 8:9],:) = [A1 A2 A3 A4 A5 A6];
  댓글 수: 1
michael hosseini
michael hosseini 2019년 7월 17일
Thank you!
I returned back variable similar to AA you mentioned, instead of the 6 variables, and it worked

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by