フィルターのクリア

How to effectively assign values to different location in a TABLE?

19 ビュー (過去 30 日間)
balandong
balandong 2017 年 8 月 3 日
コメント済み: balandong 2017 年 8 月 4 日
Dear all, Pertaining to the above subject. I want to assign the elements in vector S to the TABLE at each of the locations as defined in the vector NUMS
nums =[5;6;7;12;13;14;16;17;18];
v1_threshold = 0.1:0.1:0.2;
table = cell2table (repmat({nan},20,(numel(v1_threshold))));
S =[1,1,1,1,1,1,1,0,1];
The expected output in the TABLE should be something like
Table = {NaN;NaN;NaN;NaN;1;1;1;NaN;NaN;NaN;NaN;1;1;1;NaN;1;0;1;NaN;NaN} % Element in column 1 of TABLE
Table ={NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN;NaN} % Element in column 2 of TABLE
Initially, I thought the code should be something like
VV = num2cell (S);
for i =1:length (VV)
table (nums(i),1) = VV (i) ;
end
However, I am interested to know if there is a way to avoid the FOR loop?
I really appreciate if someone can show what I have been missing?

採用された回答

Peter Perkins
Peter Perkins 2017 年 8 月 3 日
This question is really unclear. The best I can guess is that you want to assign S into specified rows of one variable in a table that's been pre-allocated with all NaN.
The first thing, as I said in at least one other thread, is that you should stay away from cell arrays. All you have are numbers.
The second thing is don't name one of your variables "table".
The following does what my best guess is at what you want:
>> i =[5;6;7;12;13;14;16;17;18];
>> S =[1,1,1,1,1,1,1,0,1];
>> t = array2table(nan(20,3));
>> t.Var1(i) = S
t =
20×3 table
Var1 Var2 Var3
____ ____ ____
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
1 NaN NaN
1 NaN NaN
1 NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
NaN NaN NaN
1 NaN NaN
1 NaN NaN
1 NaN NaN
NaN NaN NaN
1 NaN NaN
0 NaN NaN
1 NaN NaN
NaN NaN NaN
NaN NaN NaN
  1 件のコメント
balandong
balandong 2017 年 8 月 4 日
Hi Peter Thanks for your valuable time and suggestion. Your code work like a charm.
Just a follow-up question about good MATLAB practice. Is it recommended to store the result from intermediate calculation/ process into a matrix type double or cell. Then once all the calculation is complete, only then I convert it into table type. Or, it is better to store both the intermediate and final result in the Table type. Basically, I want the end matrix in Table type.
Thanks in advance for your time and wisdom

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

その他の回答 (1 件)

Cong Ba
Cong Ba 2017 年 8 月 3 日
編集済み: Cong Ba 2017 年 8 月 3 日
%%copied from your question
nums =[5;6;7;12;13;14;16;17;18];
v1_threshold = 0.1:0.1:0.2;
table = cell2table (repmat({nan},20,(numel(v1_threshold))));
S =[1,1,1,1,1,1,1,0,1];
VV = num2cell(S);
%%assign the column without using for loop
table(nums,1) = VV';
  1 件のコメント
balandong
balandong 2017 年 8 月 4 日
Hi Cong, I really appreciate the time spent entertaining this thread.
Your suggestion work well
Thank you

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by