for loops and creating new vectors

1 回表示 (過去 30 日間)
akk
akk 2019 年 1 月 25 日
編集済み: Stephen23 2019 年 1 月 26 日
Any help with this is appreciated!
I have a matrix(mytable) that is a 128x3 double, mytable(:,1)=CO2, mytable(:,2)=year mytable(:,3)=month
I also have a matrix(k), that is a 12x2 double, k(:,1)=month, k(:,2)=diffCO2
When a value in mytable(:,3) is equal to k(:,1), I would like to take the value at mytable(:,1) - the value at k(:,2) and produce a new mytable(:,4) with the populated values.
  1 件のコメント
jahanzaib ahmad
jahanzaib ahmad 2019 年 1 月 26 日
please upload the data file . it will save my time to make data and write code .and thats the best way to get the answer .share ur code coz people like to help with code .

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

採用された回答

Stephen23
Stephen23 2019 年 1 月 26 日
編集済み: Stephen23 2019 年 1 月 26 日
You don't need any loops, just use ismember:
>> mytable = [111,11,1;222,22,2;444,44,4;666,66,6;999,99,9]
mytable =
111 11 1
222 22 2
444 44 4
666 66 6
999 99 9
>> k = [2,2222;4,4444;7,7777;9,9999]
k =
2 2222
4 4444
7 7777
9 9999
>> [X,Y] = ismember(mytable(:,3),k(:,1));
>> mytable(X,4) = k(Y(X),2)
mytable =
111 11 1 0
222 22 2 2222
444 44 4 4444
666 66 6 0
999 99 9 9999

その他の回答 (0 件)

カテゴリ

Help Center および 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