Given a vector
V = [ 19 15 11 2 16 3 1 18 14 3 18 19 20 1 13 4 14 3 1 2 16 4 3 1 19 3 20 4 13 1 15 2 18 4 1 20 19 17 3 1 13 20 3 4 17 18 19 20 14 15 11 2]
I would like to rearrange V in this way.
every 10 element I want to collect the elements that are in the same column of A
A= [0 15 11 2 4 0 3 1 13;
14 0 16 0 0 0 0 0 0;
0 0 0 0 8 0 0 0 0;
0 0 18 0 0 0 0 0 0;
0 0 19 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0];
In this case, we want to collect 11,16,18,19 and also 4,8. Doesn't mind for the others that are just one in the columns, but if we have the same number I want to collect this two number together (see below with 3)
So for example, for the first 10 elements:
19 15 11 2 16 3 1 18 14 3
they will be rearrange as
19 11 16 18 15 2 3 3 1 14

 채택된 답변

Guillaume
Guillaume 2019년 9월 16일
편집: Guillaume 2019년 9월 16일

0 개 추천

This would work as long as V has a multiple of 10 elements:
%demo data
V = [ 19 15 11 2 16 3 1 18 14 3 18 19 20 1 13 4 14 3 1 2 16 4 3 1 19 3 20 4 13 1 15 2 18 4 1 20 19 17 3 1 13 20 3 4 17 18 19 20 14 15];
A= [0 15 11 2 4 0 3 1 13;
14 0 16 0 0 0 0 0 0;
0 0 0 0 8 0 0 0 0;
0 0 18 0 0 0 0 0 0;
0 0 19 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0];
assert(mod(numel(V), 10) == 0, 'V length is not a multiple of 10');
V10 = reshape(V, 10, []); %rearrange V in batches of 10
[~, where] = ismember(V10, A); %find location of elements in A
[~, col] = ind2sub(size(A), where); %convert location into column index
[~, order] = sort(col, 1); %and sort by column order
newV = reshape(V10(order + (0:size(order, 1):numel(order)-1)), 1, []) %and rearrange columns of V10 according to that order
Note that elements of V not found in A get reordered first in each group of 10.

댓글 수: 7

luca
luca 2019년 9월 16일
if the length of V is not a multiple of 10. Is there a way to stop before 10 and close the vector without matlab gives error?
thanks
luca
luca 2019년 9월 16일
Do you have a solution?
Guillaume
Guillaume 2019년 9월 16일
편집: Guillaume 2019년 9월 16일
If you expect instantaneous answers, then this forum is not for you. We help in our own time.
If the length of V is not a multiple of 10, the simplest may be to pad it with NaN:
V = [V, NaN(1, mod(-numel(V), 10))];
%... code as above
newV = V(1:numel(V)) %to remove the added NaN (which will always be sorted to the end)
luca
luca 2019년 9월 16일
Sorry Guillaume,
I didn't want to rush you! Pardon me and thanks for your help!
luca
luca 2019년 9월 16일
The fact to insert NaN complicate and create confusion once we have to reorder. If you have other suggestion on how to do it, please let me know!
Thanks
Oh, yes, forgot I'm not sorting the numbers but their column position (which is 0 for NaNs).
newV = V(~isnan(V))
for the last line. The rest as before.
luca
luca 2019년 9월 16일
thanks again for your time

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 9월 16일

댓글:

2019년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by