Create an array from another and find the indices.

조회 수: 3 (최근 30일)
João
João 2014년 2월 11일
댓글: Jos (10584) 2014년 2월 11일
Hi everyone,
I have an array with variables like S,SV, V etc now I want to create a new array only with the V variable so for example;
V
SV
S
V
will become
V
V
after this I need to know the indices where I found each one of the V variables, so in this example I should get Indices = 1 and 4
Thank you for the help.
  댓글 수: 2
Walter Roberson
Walter Roberson 2014년 2월 11일
Are those strings, or are they numeric values? If they are numeric values, are they scalars or vectors or arrays? If they are vectors or arrays are all of them the same size?
João
João 2014년 2월 11일
Thx for answering. This is an array of strings I already tried to use an if cycle like this;
%most_common_string
for i=1:744;
if most_common_string{i} == 'V'
periodo=most_common_string{i};
end
end
but instead of getting a new array I'm getting only a cell.

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

채택된 답변

Jos (10584)
Jos (10584) 2014년 2월 11일
MySet = {'S' 'V' 'SV' 'S' 'V' 'V' 'S' 'SV' 'V'}
MyStr = 'V'
tf = strcmp(MySet, MyStr)
index = find(tf)
KeepSet1 = MySet(tf)
KeepSet2 = repmat(MyStr,1,numel(index)) % same result
  댓글 수: 2
João
João 2014년 2월 11일
편집: João 2014년 2월 11일
Thank you Jos, KeepSet1 is giving me all the V's but KeepSet2 is giving me an array only with VVVVVVVVVV(...) so for that I used
ind = cellfun(@(s) strcmp(s,'V'), most_common_string)
find(ind)
Jos (10584)
Jos (10584) 2014년 2월 11일
Oh, I made a small mistake. I just wanted to point out that what you want to keep are only the V's. There are numel(index) elements to keep. These elements can be retrieved from MySet using the Index OR by repeating the element a number of times. The correct code, however, is:
KeepSet2 = repmat({MyStr},1,numel(index)) % between { }

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2014년 2월 11일
find( ismember('V', most_common_string))
  댓글 수: 1
João
João 2014년 2월 11일
Thanks for answering Walter, but when I use find I only get one index, the first index.
Maybe I was not clear, I will need to use those indices later so maybe the best is to get an array with each one of the indices values.

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


David Sanchez
David Sanchez 2014년 2월 11일
most_common_string = {'V','SV','S','V','S'};
wanted_string='V';
idx = getnameidx(most_common_string,wanted_string);
idx = find(most_common_string==wanted_string);
k=1;
for i=1:numel(most_common_string)
if most_common_string{i} == 'V'
periodo(k)=i;
k = k+1;
end
end
periodo =
1 4
new_string_array = most_common_string(periodo);
new_string_array =
'V' 'V'
  댓글 수: 1
João
João 2014년 2월 11일
Thanks for your help but I'm getting this error
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Error in ==> Untitled at 110
idx = find(M==wanted_string);
if true
% code
end

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by