필터 지우기
필터 지우기

concatenate

조회 수: 3 (최근 30일)
God'sSon
God'sSon 2011년 3월 28일
편집: Rajeev 2014년 5월 26일
I have a for-loop that executes commands and displays the answer for each iteration. Please, can someone tell me how to concatenate all the answers into an array instead of having the results displayed independently for each iteration. cheers.
  댓글 수: 2
Rajeev
Rajeev 2014년 5월 26일
hey me too searching for the same question did u get the answer?
Rajeev
Rajeev 2014년 5월 26일
편집: Rajeev 2014년 5월 26일
if u store ur answer in A. then give A as A[ ]; in array form it will store automaticly

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

채택된 답변

Walter Roberson
Walter Roberson 2011년 3월 28일
Paulo answered the question as you phrased it, but if you know the exact number of results you are producing (or if the maximum number of results is "reasonably close" to the likely number of results), then you are better off pre-allocating memory for the results:
a = zeros(10,1);
for b = 1:10
a(b) = 3*b+5; %for example
end
When you get to thousands of elements, pre-allocating can be much much faster.
  댓글 수: 2
God'sSon
God'sSon 2011년 3월 28일
thanks for your answer.
I fancy the idea of pre-allocating memory but I couldnt implement it with my code.
consider this example:
a= 2 1 3 1
1 4 1 5
6 7 1 1
8 1 9 1
for i=(1:4)
row=(a(i,:));
b=sort(row)
end
I need an answer which is (b= 4x4 array) like the original (a)
cheers.
Walter Roberson
Walter Roberson 2011년 3월 28일
b = zeros(size(a));
for i = 1:size(a,1)
b(i,:) = sort(a(i,:))
end
Or more simply,
b = sort(a,2);

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

추가 답변 (1개)

Paulo Silva
Paulo Silva 2011년 3월 28일
a=[]
for b=1:10
a=[a b] %replace the b with the data you want to store per iteration
end
a %result is inside this variable
The code doesn't do any memory pre-allocation!!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by