# Deleting an irregular "row"/"column" from 2-d array

7 ビュー (過去 30 日間)
Knut 2017 年 12 月 5 日

I have a 2d array, and I want to eliminate the same number of elements in each row (or column) (e.g. 1 element), but the index of that element is itself av vector. This operation would be relevant for doing seam-carving (content-aware image resize).
Is there any neat way of doing this without my array collapsing into a 1d vector?
A = (1:3)'*(1:3);
A =
1 2 3
2 4 6
3 6 9
b = [1; 2; 2];
for r=(1:3)
tmp = A(r,:);
tmp(b(r)) = [];
Y(r,:) = tmp;
end
Y =
2 3
2 6
3 9
I seem to have a working method, but it is kind of cumbersome as I have to (?) make sure that the linear indexing into Y is strictly monotonic:
Y = A';
Y(sub2ind(size(A'), b, (1:3)')) = [];
Y = reshape(Y, size(A,2)-1, size(A,1))'
Anything neater?

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

### 回答 (1 件)

Matt J 2017 年 12 月 5 日

I think your solution is probably the best, except that I would avoid the extra calls to size() and ctranspose(). The latter are particularly expensive.
Y = A.';
[m,n]=size(Y);
Y( sub2ind([m,n], b, (1:3)) )=[];
Y = reshape(Y, m-1, n).'

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

### Community Treasure Hunt

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

Start Hunting!
{"ft_lang_label":"__label__en","ft_lang_prob":0.8340863,"math_prob":0.9853994,"size":733,"snap":"2020-45-2020-50","text_gpt3_token_len":250,"char_repetition_ratio":0.08367627,"word_repetition_ratio":0.0,"special_character_ratio":0.3819918,"punctuation_ratio":0.17708333,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9842832,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-27T12:55:29Z\",\"WARC-Record-ID\":\"<urn:uuid:e5780def-1aab-4cf5-81c7-0e64523b0e82>\",\"Content-Length\":\"105618\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c5d6818a-2369-4d5d-8eb7-68c92bd2635b>\",\"WARC-Concurrent-To\":\"<urn:uuid:1773368c-ecc7-4620-b217-f6415374688a>\",\"WARC-IP-Address\":\"23.223.252.57\",\"WARC-Target-URI\":\"https://jp.mathworks.com/matlabcentral/answers/371292-deleting-an-irregular-row-column-from-2-d-array?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:SUMC7CNE3VAIKVP5CR4H23HG5KV5LMUI\",\"WARC-Block-Digest\":\"sha1:VMTUGPOPU6K6EENOUUMFTFWZ7SLTPJYZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141191692.20_warc_CC-MAIN-20201127103102-20201127133102-00122.warc.gz\"}"}