Appending vectors in a loop

2 Ansichten (letzte 30 Tage)
T
T am 17 Aug. 2013
Data1 =
4
5
6
7
8
9
10
11
12
-1
4
5
6
7
8
9
10
11
12
-1
4
5
6
7
8
9
10
11
12
-1
4
5
6
7
8
9
10
11
12
-1
Data2= Columns 1 through 2
[ 4] [0.0119]
[ 5] [0.0119]
[ 6] [0.0143]
[ 7] [0.0143]
[ 8] [0.0187]
[ 9] [0.0256]
[10] [0.0273]
[11] [0.0119]
[12] [0.0143]
I want to be able to take in values from data2 for column 2 using the corresponding values in column 1 and data1 . I would use the following command:
find(ismember(cell2mat(data2),data1)==1);
The problem, if you look closely at the first set, is that there is a -1. I need to force it’s value to be zero. Thus I would have to append the vector.
If the command about is within a loop, how do I append data2 without prompting an error?
  2 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 17 Aug. 2013
What are you expecting as result, you can make your example shorter
T
T am 18 Aug. 2013
Bearbeitet: T am 18 Aug. 2013
I am expecting:
Data2=
[ 4] [0.0119]
[ 5] [0.0119]
[ 6] [0.0143]
[ 7] [0.0143]
[ 8] [0.0187]
[ 9] [0.0256]
[10] [0.0273]
[11] [0.0119]
[12] [0.0143]
[-1] [ 0 ]
and I want to be able to retrieve the second column in Data1 with each corresponding value in column 1 of Data2.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 17 Aug. 2013
Bearbeitet: Azzi Abdelmalek am 17 Aug. 2013
out=Data2(ismember(cell2mat(Data2(:,1)),Data1),:)
% to change values of Data1 from -1 to 0
Data1(Data1==-1)=0
  8 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 19 Aug. 2013
find(ismember([a{:}],[Data1{:}]))
T
T am 19 Aug. 2013
Bearbeitet: T am 19 Aug. 2013
find(ismember([a{:}],[Data1{:}])==1) is what I needed, thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 18 Aug. 2013
Bearbeitet: Andrei Bobrov am 18 Aug. 2013
Data2 = [...
4 0.0119
5 0.0119
6 0.0143
7 0.0143
8 0.0187
9 0.0256
10 0.0273
11 0.0119
12 0.0143]
out = Data1*[1 0];
[l,ii] = ismember(Data1,Data2(:,1));
out(l,2) = Data2(ii(l),2);
  2 Kommentare
T
T am 18 Aug. 2013
Where did you take into account -1?
Andrei Bobrov
Andrei Bobrov am 19 Aug. 2013
in out = Data1*[1 0];

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by