How to cut and add values from every iteration. Please Help

p = [90 90 -45 0 0 45 45 0 -45]; p1 = p; %store temporary copy of p
p_descend = sort(p, 'descend');
p_ascend = sort(p,'ascend');
idx = [true diff(p)~=0];
q = p(idx); q1=q; %temporary copy of q
idx = fliplr([true, diff(fliplr(p))~=0]);
position_p = find(idx); pos = position_p;
ii = find(idx);
n = numel(ii);
layer1 = zeros(n,numel(p));
layer1(1,:) = p;
i1 = fliplr(ii);
for j = 2:n
layer1(j,:) = layer1(j-1,:);
layer1(j,[i1(j),end]) = layer1(j,[end,i1(j)]);
end
layer = reshape(layer1(2:end,:)',1,size(layer1,2),(size(layer1,1)-1));
I want to store last value of layer
means values should be
out(:,:,1) = [0];%add last value from every iteration
out(:,:,2) = [0 45];%last value of iteration 1 & 2
out(:,:,3) = [0 45 0];
out(:,:,4) = [0 45 0 -45];
out(:,:,5) = [0 45 0 -45 90];
and i need last values from every iterations

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 1월 26일
편집: Andrei Bobrov 2016년 1월 26일
Hi Ranjan! Edited loop for..end in your code.
output1 = cell(n-1,1);
for jj = 2:n
layer1(jj,:) = layer1(jj-1,:);
layer1(jj,[i1(jj),end]) = layer1(jj,[end,i1(jj)]);
output1{jj-1} = layer1(jj,(end-jj+2):end);
end

댓글 수: 1

Triveni
Triveni 2016년 1월 27일
편집: Triveni 2016년 1월 27일
out1(:,:,1) = [0];%add last value from every iteration
out1(:,:,2) = [0 45];%last value of iteration 1 & 2
out1(:,:,3) = [0 45 0];%last value of iteration 1,2&3
out1(:,:,4) = [0 45 0 -45];%last value of iteration 1,2,3&4
out1(:,:,5) = [0 45 0 -45 90];%last value of iteration 1,2,3,4&5
?? and please correct this too
cell2mat(reshape(output1,1,1,size(output1,1)),1,[],[])
Thanks sir....You have done lots for me....you have given full support in my thesis.

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

추가 답변 (0개)

카테고리

제품

태그

질문:

2016년 1월 26일

편집:

2016년 2월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by