How to save output results from loop in a vectors?

2 ビュー (過去 30 日間)
Bojan
Bojan 2014 年 3 月 25 日
回答済み: Ibrahim Mohammed Wajid 2022 年 6 月 26 日
So this is my code:
threeminutes_Facebook_newsfeed;
h=[0:0.5:179];
for idx=1:length(h)
threshold=h(idx);
m =find(threshold-1<=x & x<=threshold);
Y(idx)=sum(y(m));
end
count_zero=0;
count_value=0;
i=1;
while i<length(Y)
if (Y(i)==0)
count_zero=count_zero+1;
i=i+1;
while Y(i)==0 && i<length(Y)
count_zero=count_zero+1;
i=i+1;
end
if i<(length(Y))
count_zero
count_zero=0;
end
if i==(length(Y)) && Y(length(Y))~=0
count_value=1;
count_value
count_value=0;
elseif i==(length(Y)) && Y(length(Y))==0
count_zero=count_zero + 1;
count_zero
count_zero=0;
end
else
count_value=count_value+1;
i=i+1;
while Y(i)~=0 && i<length(Y)
count_value=count_value+1;
i=i+1;
end
if i<(length(Y))
count_value
count_value=0;
end
if i==(length(Y)) && Y(length(Y))~=0
count_value=count_value+1;
count_value
count_value=0;
elseif i==(length(Y)) && Y(length(Y))==0
count_zero=1;
count_zero
count_zero=0;
end
end
end
I have multiple results for count_zero and count_values. I would like the results are in vectors, for exaple all results of count_zero in vector a, and all results from count_values in vector b. I know it is possible just cannot do it. Please help! Thanks in advance!

回答 (1 件)

Ibrahim Mohammed Wajid
Ibrahim Mohammed Wajid 2022 年 6 月 26 日
  1. Initialize two vectors a and b as a=[],b=[];
  2. Now add this line (a = [a count_zero];)at a line or end of loop from where you need to store the value of count_zero from.
  3. Do the same for b,(b = [b count_value];) .
  4. This will store all your values of count_zero in vector a and count_value in vector b.
  5. Below is the example for your reference.
Given a collection of points, return the indices of the rows that contain the two points most distant from one another. The input vector p has two columns corresponding to the x and y coordinates of each point. Return ix, the (sorted) pair of indices pointing to the remotest rows. There will always be one unique such pair of points.
So if
p = [0 0]
[1 0]
[2 2]
[0 1]
Then
ix = [1 3]
That is, the two points p(1,:) and p(3,:) are farthest apart.
%Then
% ix = [1 3]
%That is, the two points p(1,:) and p(3,:) are farthest apart.
function ix = mostDistant(p)
[len,~] = size(p);
dist = []; % vector I am storing distance for each combination of points
idx = []; % indices of points for which the distance is stored in vector dist
for i = 1: len
for j = 1:len
distance_between = (p(j,1)-p(i,1))^2 + (p(j,2)-p(i,2))^2;
distance_between;
dist = [dist; distance_between; distance_between]; % Appending the distance
idx = [idx ;i ;j]; % Appending the indices
end
end
mat = [dist idx];
h = sortrows(mat,1,"ascend");
[len2 ,~] = size(h);
iwx = [h(end-1,2) h(end,2)];
ix = sort(iwx);
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by