Get adjacent array value if condition met

조회 수: 1 (최근 30일)
Vineet Guru
Vineet Guru 2013년 5월 22일
Hi, I have a problem which I have been struggling with for a while and was wondering if some kind soul can help me out.
I have an array of two columns. The values in the first are incrementing integers up to 20 (so 1 to 20 in increasing order). The second column is a set of random integers. So each vlue in cloum 1 is matched with a random integer in column 2. I have called this array H.
What I seek help on is this: I need to create a new array of values in column 1 that match a specific number from cloumn B. So the new array, I call z, holds only those values where its corresponding vlue in column 2 are all the same.
The code I wrote is this:
z= [];
m = 20;
for i=1:1:m
if H(:,2)== 3 %I would like to create an array 'z' of all values
%in col 1 which has 3 as a corresponding value
% in col 2.
z = H(:,1);
z = [z;z];
end
end
I
  댓글 수: 1
Iain
Iain 2013년 5월 22일
Have a look at the "unique" function.

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

채택된 답변

the cyclist
the cyclist 2013년 5월 22일
z = H(H(:,2)==3,1);
  댓글 수: 1
Vineet Guru
Vineet Guru 2013년 5월 22일
Brilliant!!! It works. Many thanks. Didn't know why I was tying myself in knots.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by