emptying each row of matrix of a in a loop then uses respective matrices produced from it to get a certain value

1 次查看(过去 30 天)
I have a matrix, a
a = [1 2;
1 3;
1 5;
1 7;
1 8;];
and this matrix a, is emptied in each row per loop until the last one using this code from Matt J
a0=a;
for i=1:size(a,1)
a=a0;
a(i,:)=[],
end
which result in:
a = [1 3;
1 5;
1 7;
1 8;];
a = [1 2;
1 5;
1 7;
1 8;];
a = [1 2;
1 3;
1 7;
1 8;];
a = [1 2;
1 3;
1 5;
1 8;];
a = [1 2;
1 3;
1 5;
1 7;];
can I create another loop to calculate a certain value, w, from the results from each loop beginning with the orginal until the last one. After each loop answer value, w, is tabulated in matrix b
b = [w
w
w
w
w
w]
there 6 values of w since we consider the orginal a
  4 个评论
JL
JL 2019-8-22
for i=1:size(a,1)
a=a0;
a(i,:)=[],
% work with matrix a here
end
This code actually works but it doesnt save the values from this loop into another matrix

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2019-8-22
编辑:Matt J 2019-8-22
Why not as follows,
b=nan(size(a,1),1); %pre-allocate
for i=1:size(a,1)
tmp=a;
tmp(i,:)=[],
b(i)=max(tmp(:))-min(tmp(:)); %example derived quantity
end
  5 个评论
David Hill
David Hill 2019-8-22
best to pre-allowcate as Matt discribed. you could b=[]; outside loop and b=[b,w];inside loop. Matt's solution works best.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by