フィルターのクリア

How to efficiently replace value at the n last column?

1 回表示 (過去 30 日間)
balandong
balandong 2018 年 9 月 4 日
コメント済み: balandong 2018 年 9 月 4 日
Dear all, The objective was to replace the value at the end of a column. I have make a simple case but wonder if there is another way to make it more compact and efficient?
Thanks in advance
f_sbj=2
sA=23;
A =1:sA;
appndOut=cell(sA,1);
for f_x=1:sA
Out = nchoosek(A, f_x);
[rows, columns] = find(Out==f_sbj);
Out(rows, :) = [];
[m,n] = size(Out );
newNan=nan(m,(sA-n));
appndOut{f_x}=[newNan Out];
end
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 4 日
That code appears to insert at the beginning of columns, not replace at the end of columns.
The code can be made more compact, but not necessarily more efficient.
balandong
balandong 2018 年 9 月 4 日
Thanks for the quick response, Appreciate if you can some idea to make it more compact

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

採用された回答

Walter Roberson
Walter Roberson 2018 年 9 月 4 日
lastNcol = @(M, N) M(:,end-N+1:end);
NanPrePad = @(M, N) lastNcol( [nan(size(M,1), N), M], N);
select_and_prepad = @(M, N) NanPrePad( M(all(M~=f_sbj, 2),:) );
appndOut = arrayfun(@(f_x) select_and_prepad(nchoosek(A, f_x), sA), (1:sA).', 'uniform', 0);
You can use shorter names for the anonymous functions if you find the above to not be compact enough.
Though I wonder why you do not do something like
A = setdiff(1:sA, f_sbj);
That would eliminate the possibility of f_sbj being generated by the nchoosek, so it would not be necessary to delete any rows containing f_sbj.
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 9 月 4 日
f_sbj=2
sA=23;
A =1:sA;
lastNcol = @(M, N) M(:,end-N+1:end);
NanPrePad = @(M, N) lastNcol( [nan(size(M,1), N), M], N);
select_and_prepad = @(M, N) NanPrePad( M(all(M~=f_sbj, 2),:), N );
appndOut = arrayfun(@(f_x) select_and_prepad(nchoosek(A, f_x), sA), (1:sA).', 'uniform', 0);
This takes a moment to execute.
balandong
balandong 2018 年 9 月 4 日
Works like a charm, Thanks Walter

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by