필터 지우기
필터 지우기

how to compare an element of array to the rest of the elements

조회 수: 17 (최근 30일)
aboltabol
aboltabol 2015년 9월 26일
댓글: aboltabol 2015년 9월 26일
I am trying to compare each element of an array with its subsequent elements and get the count of number of times the current element is greater than the subsequent elements.
the following is my code
function [ z ] = test(i,size_data, num,j,k,sum )
% test
num=xlsread('MKdata.xlsx');
size_data=size(num);
j=0
k=0
i=1
for (i=i:size_data)
if num(i) > num([i+1:size_data])
j=j+1
else k=k-1
end
i=i+1
end
sum=j+k
end
the test data set i am using is
1733 3002 875 164 1464 1399 1039 1096 1812 2347 575 1393 411 283 1407 1294 1102 1886 3058 4587
results shows that every increment of i adds 1 to k and finally i get k=-20, j=0 whereas it should be 26
please guide me, please point out the mistake in logic or syntax

채택된 답변

Image Analyst
Image Analyst 2015년 9월 26일
Wouldn't you just do this (vectorized):
% Define some arbitrary random data
data = randi(9, 1, 20)
% Define the element we're going to consider
index = 3;
% Compare each element of an array with its subsequent elements
% Get logical index that is true when current element is greater.
itIsGreater = data(index) > data(index+1:end)
% Get the count of number of times the current element is greater than the subsequent elements.
theCount = sum(itIsGreater)
You didn't say in words that you want to do that for all possible starting indexes, but if you do, then put it in a loop and make the count depend on starting index.
% Define some arbitrary random data
data = randi(9, 1, 20)
theCount = zeros(1, length(data));
for index = 1 : length(data)
% Compare each element of an array with its subsequent elements
% Get logical index that is true when current element is greater.
itIsGreater = data(index) > data(index+1:end)
if ~isempty(itIsGreater) % if there is at least 1 greater...
% Get the count of number of times the current element is greater than the subsequent elements.
theCount(index) = sum(itIsGreater)
end
end
% Print to command window..
theCount

추가 답변 (2개)

Walter Roberson
Walter Roberson 2015년 9월 26일
When you have
if num(i) > num([i+1:size_data])
then you are comparing one value to a number of values, which will give you a vector of logical results, 0 for any places it does not hold and 1 for any places it holds. "if" is only considered true if all of the elements it is asked to process are true, so your "if" would be considered true only if num(i) was greater than all of the remaining elements in num.
I cannot tell you how to correct your code because you neglected to indicate the circumstances under which you want j or k to be incremented.
Meanwhile I recommend you look at the documentation for all() and for any()
  댓글 수: 1
aboltabol
aboltabol 2015년 9월 26일
Sir, Thanks for your reply I want j to be incremented each time an element, to which num(i) is being compared, is greater than num(i). if the element is less than num(i) then i want k to be incremented. this has to be done for each element, for all its subsequent elements. so for a series having 20 entries the first entry will be compared to the following 19 entries and the 19th entry will be compared to 20th entry only

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


Jos
Jos 2015년 9월 26일
As Walter said num(i)>num(i+1:end) compares num(i) to all subsequent elements with result 1 for each time true. You can use this by summing the result, which will give what you want. The code below gives your 26 as answer
size_data=size(num,2);
j=0;
k=0;
for i=1:size_data-1
j = j + sum(num(i)>num(i+1:end));
k = k + sum(num(i)<num(i+1:end));
end
sum=k-j;

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by