indexing into an array

3 views (last 30 days)
Patrick Benz
Patrick Benz on 20 Sep 2021
Commented: Patrick Benz on 20 Sep 2021
I know this is probably one of the easiest thing to do, but right now, I am stuck at it. I have got 2 Arrays. One is 55618x4 (A) and the other one is 1819x1 (B).
A
1 -20.8047047000000 -9 9
2 -20.8047047000000 -9 -9
3 20.8047047000000 -9 -9
4 20.8047047000000 -9 9
5 -20.8047047000000 9 -9
6 20.8047047000000 9 -9
7 20.8047047000000 9 9
8 -20.8047047000000 9 9
9 41.6427193000000 9 -9
B
514
515
516
517
518
519
520
According to the number in the first column, the information from the three other columns should be written from array A to array B. So I need something like this:
C
514 41.64271 8.5 0
515 40.46843 7.3 5
With
[LIA,LOCB]=ismember(s11(:,1),Coords(:,1));
I can find the rows I need, but how can I fill the array?
  2 Comments
Steven Lord
Steven Lord on 20 Sep 2021
It's not at all clear how you got from A and B to C. Why do you take data from the 1st row of B and the 9th row of A (I assume) to generate the 1st row of C? And where did 8.5 and 0 come from? Neither of those numbers appear in either A or B.
Please explain the rules you want to use to create C more thoroughly and clearly.
Patrick Benz
Patrick Benz on 20 Sep 2021
I just used a bad extract from A
514 19.4228840000000 18.8586121000000 0
515 18.0399971000000 18.7197781000000 0
516 16.6561203000000 18.5911980000000 0
517 15.2713270000000 18.4728775000000 0
518 13.8856945000000 18.3648243000000 0
519 12.4993000000000 18.2670441000000 0
520 11.1122189000000 18.1795425000000 0
These are the Data from A that I want in C.
So more or less all I need are the 1819 relevant rows extracted from A accrording to the numbers from B

Sign in to comment.

Accepted Answer

David Hill
David Hill on 20 Sep 2021
[~,idx]=ismember(B,A(:,1));
C=A(idx,:);
  1 Comment
Patrick Benz
Patrick Benz on 20 Sep 2021
Nice, thank you very much. Works perfectly. I tried it with another array which looks like this:
1 202 1767 5805 1245 1 33 723 108 14243 14242 14241 14240 14244 14245 14246
14247 14249 14248 14250 14251
2 1767 1768 5806 5805 33 34 724 723 14254 14253 14252 14242 14255 14256 14257
14245 14248 14258 14259 14250
3 1768 1769 5807 5806 34 35 725 724 14262 14261 14260 14253 14263 14264 14265
14256 14258 14266 14267 14259
4 1769 1770 5808 5807 35 36 726 725 14270 14269 14268 14261 14271 14272 14273
14264 14266 14274 14275 14267
5 1770 1771 5809 5808 36 37 727 726 14278 14277 14276 14269 14279 14280 14281
14272 14274 14282 14283 14275
The Problem here is the formatting of the input data. There are always 2 rows that belong to 1 Number. For example the numbers 202 1767 5805 .... up until 14251 belong to the "1"
[~,idx]=ismember(Node_Coord(:,1),Elemente(:,:));
I get the index of every Node in this list of Nodes per Element.
Is it possible to find the row according to the index?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!