필터 지우기
필터 지우기

How to Save multiple results into one array

조회 수: 12 (최근 30일)
Hans123
Hans123 2020년 6월 11일
편집: madhan ravi 2020년 6월 11일
This seems simple, but I can't wrap my head around it
I am trying to save 2 variables that are output from the mink, where k=2, function into a single array
the results should be consecutive and be in a horizontal array, such that - for the first iteration the output was 4 and 6 and the second iteration the outputs are 8 and 9
so the array should be xval = [4,6,7,8..]
Below is the code I was working with however it did not work, any help is appreciated
xvalue=zeros(2*length(x),1);
for i=1:length(x)
xvalue(i)=mink(abs(Lx-x(i)),2); %
end

채택된 답변

Stephen23
Stephen23 2020년 6월 11일
n = numel(x);
xvalue = zeros(2,n);
for ii = 1:n
xvalue(:,ii) = mink(abs(Lx-x(ii)),2);
end
xvalue = xvalue(:).'

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 6월 11일
편집: madhan ravi 2020년 6월 11일
xvalue = cell(numel(x),1);
for ii = 1:numel(x)
xvalue(ii) = mink(abs(Lx-x(ii)),2);
end
celldisp(xvalue)
cat(2,xvalue{:})
  댓글 수: 2
Hans123
Hans123 2020년 6월 11일
thanks for the input, madhan
I get an error that I have pasted below, could you break down this process - also is there a method that circumvents the use of cells
Conversion to cell from double is not possible.
Error in load_point_charges (line 65)
xvalue(ii) = mink(abs(Lx-x(ii)),2);
madhan ravi
madhan ravi 2020년 6월 11일
편집: madhan ravi 2020년 6월 11일
xvalue{ii} % had to use {}

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by