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

1 view (last 30 days)
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

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 26 Jan 2016
Edited: Andrei Bobrov on 26 Jan 2016
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 Comment
Triveni
Triveni on 27 Jan 2016
Edited: Triveni on 27 Jan 2016
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.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!