Appending vectors in a loop

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 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 17 日
What are you expecting as result, you can make your example shorter
T
T 2013 年 8 月 18 日
編集済み: T 2013 年 8 月 18 日
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.

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

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 17 日
編集済み: Azzi Abdelmalek 2013 年 8 月 17 日

0 投票

out=Data2(ismember(cell2mat(Data2(:,1)),Data1),:)
% to change values of Data1 from -1 to 0
Data1(Data1==-1)=0

8 件のコメント

T
T 2013 年 8 月 18 日
編集済み: Azzi Abdelmalek 2013 年 8 月 19 日
So if I use
negativeID = [Data2; [{1},{0}]
That gives me
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 ]
However, I get an error when I use
out=negativeID(ismember(cell2mat(negativeID(:,1)),Data1),:)
It says All contets of the input cell array must be of the same data type.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 18 日
You are missing a bracket
negativeID = [Data2; [{1},{0}]]
T
T 2013 年 8 月 18 日
編集済み: Azzi Abdelmalek 2013 年 8 月 19 日
That was a typo but it still gives me the same result.
Nevertheless,
if I have
negativeID(:,1) =
[ 4]
[ 5]
[ 6]
[ 7]
[ 8]
[ 9]
[10]
[11]
[12]
[-1]
and use cell2mat(negativeID(:,1))
then I get the error I mentioned, why would it work without the -1???
T
T 2013 年 8 月 19 日
The problem is that with 4-12 is a different data type and -1 is just an integer when I appended the vector. How do I find out what data type the cell is?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 19 日
Copy and past this code
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={[ 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 ]}
negativeID = [Data2; [{1},{0}]]
a=negativeID(:,1);
out=Data2(ismember([a{:}],[Data1{:}]),:)
T
T 2013 年 8 月 19 日
編集済み: Azzi Abdelmalek 2013 年 8 月 19 日
That works but I cannot get the indices for each value.
find(Data2(ismember([a{:}],[Data1{:}]),:)) says Undefined function or method 'find' for input arguments of type 'cell'.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 19 日
find(ismember([a{:}],[Data1{:}]))
T
T 2013 年 8 月 19 日
編集済み: T 2013 年 8 月 19 日
find(ismember([a{:}],[Data1{:}])==1) is what I needed, thanks.

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 8 月 18 日
編集済み: Andrei Bobrov 2013 年 8 月 18 日

0 投票

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 件のコメント

T
T 2013 年 8 月 18 日
Where did you take into account -1?
Andrei Bobrov
Andrei Bobrov 2013 年 8 月 19 日
in out = Data1*[1 0];

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by