フィルターのクリア

Locating characters in a string array

31 ビュー (過去 30 日間)
Scott Dean Seedell
Scott Dean Seedell 2020 年 6 月 28 日
コメント済み: dpb 2020 年 11 月 11 日
Hi,
I've generated a list of numerous characters in a string (102) and assigned each character a number (1:102) in the second column to form a 102x2 string array.
What I want to do is seach for a character (column 1) and obtain the number it is assigned (column 2).
Causing me some headaches - thanks in advance!

回答 (3 件)

dpb
dpb 2020 年 6 月 28 日
As the other respondent says, you don't need a corollary array unless the lookup isn't 1:1 with position (which your description implies it isn't).
The builtin functions for the purpose are strfind and contains for simple pattern matching; there's regexp for regular expressions.
See the documentation for strings, cell strings and character string arrays in the section of "Data Types" in the documentation for complete discussion of the varied ways to deal with text data in MATLAB.

RAVIKIRAN YALAMARTHI
RAVIKIRAN YALAMARTHI 2020 年 6 月 28 日
In first step you are creating a string whose length is 102.
In second step you are creating a array of 1:102. But, in a string each character has its own index value. i.e first character index=1 and last character index=102. So, there is no need to create a another column to find corresponding number.
we can find the answer with the following example code.
x = 'Scott Dean Seedell'
y = find(x=='S')
answer will be: y = 1 12
i.e. corresponding numbers of character 'S' are 1 and 12.
  15 件のコメント
Scott Dean Seedell
Scott Dean Seedell 2020 年 7 月 15 日
Sorry, I didn't know that was possible.
Does the attached make it possible to see better?
Thanks
dpb
dpb 2020 年 7 月 15 日
Well, let me go look and see...I'm sure it will. Storm last night took out the line (wind broke off a dead limb that had been intending to get down) that feeds the well so have to get finished putting it all back first...

サインインしてコメントする。


dpb
dpb 2020 年 7 月 16 日
OK, got the electrical problem in stable state for time being with power back the main well so cattle are drinking...shop not on but can live without the old shop temporarily...
Anyway, I'd do something like this with your Line_Bus array but wonder why there's so much duplicated and seemingly just sequentially-numbered data. Seems like a lot of redundancy in the duplicated data department of redundancy department, but maybe this is just demo data??? That aside--
Line_bus=categorical(Line_bus); % make them all categorical() arrays
fnLocateLine=@(l) find(Line_busC(:,4)==l); % define a lookup function for shorthand
% Illustrate using lookup...
>> ix=fnLocateLine('line10') % return index of given location
ix =
9
>> Line_busC(fnLocateLine('line10'),:) % return the data associated with the index...
ans =
1×4 categorical array
8 9 9 line10
>>
It possibly could be more useful to leave the numeric columns as numeric but if they're just IDs, the categorical is probably better than string.
  10 件のコメント
Scott Dean Seedell
Scott Dean Seedell 2020 年 11 月 10 日
I managed to resolve it (was actually quite simple in the end, I was looking for something that wasn't there).
As you're rather helpful though, I'm having some difficulty plotting a surface with the results. I've attached 'C' from which I'm trying to obtain a 3D surface plot with column 3 as the Z axis and 1&2 as X and Y. It's coming out all wrong though - any ideas?
dpb
dpb 2020 年 11 月 11 日
MATLAB surface plots are pretty restrictive -- must have X,Y data in meshgrid form with a corresponding Z 2D matrix; it doesn't have the facilities to handle columnar Z (as you've undoubtedly discovered)
You would have to create a meshgrid and fill in with interpolation to use those; however, you can visualize the data with scatter3 which will accept the vector form--doing that for your data results in:
There's really no evidence of any surface at all, anyway -- I then followed up with the marginal plots of Z vs X and Y alone and they also are basically random with only a slight preponderance to more values centered around Z=3E4 than higher, but still there's no indication of any real pattern that a surface would fit.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by