loop with easy underfunction

조회 수: 9 (최근 30일)
ErikJ
ErikJ 2017년 11월 7일
댓글: Rik 2017년 11월 7일
function tretti
for k=1:3
F=results(k)
end
function h=results(k)
A ={k*2};
A1(k)= A
h=A1
%output: F=[] [] [6]
%what i want: F=[2] [4] [6] //cell array
  댓글 수: 1
ErikJ
ErikJ 2017년 11월 7일
Sorry, the question is: why is my underfunction not remembering my earlier value of A1 and how can i make it to remember?

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

답변 (1개)

Rik
Rik 2017년 11월 7일
Because you are overwriting it every iteration.
function F=tretti
for k=1:3
F{k}=results(k);
end
function h=results(k)
A ={k*2};
A1(k)= A;
h=A1;
You should look into cellfun, it may be really useful for what you are trying to do.
  댓글 수: 2
ErikJ
ErikJ 2017년 11월 7일
yes but i dont have a cell array that i want to aply a function on. I want to make a cell array [2] [4] [6] [8] [10].... cellfun(func,A) my function is take k*2 but what is my A and how do i create it?
Rik
Rik 2017년 11월 7일
The modification of your code should work. You could also generate it as a vector or matrix, and then convert it to a cell with num2cell (or mat2cell).

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by