I have the matrix a=[5 5 10 10 2 4 5],I want the result to be d=[5 1 2 7;10 3 4 0;2 5 0 0;4 6 0 0],

I have the matrix
a=[5 5 10 10 2 4 5],
I want the result to be
d=[ 5 1 2 7;
10 3 4 0;
2 5 0 0;
4 6 0 0],

3 件のコメント

the cyclist
the cyclist 2013 年 11 月 25 日
It is not at all clear to me how d relates to a .
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 25 日
How?
yousef Yousef
yousef Yousef 2013 年 11 月 25 日
the first column indicates the value.The other columns show the indecis for ex. 5 exists in 1,2&7

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

回答 (4 件)

How about this:
a=[5 5 10 10 2 4 5]
d = transforma(a); % Call transforma() in the main program.
function d = transforma(a)
d=[ 5 1 2 7;
10 3 4 0;
2 5 0 0;
4 6 0 0];
It meets all the criteria you've given.

4 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 25 日
Nice answer!
yousef Yousef
yousef Yousef 2013 年 11 月 25 日
There is not such command in Matlab(transforma)
There is once I defined it. Did you overlook the
function d = transforma(a)
statement where I defined it?
yousef Yousef
yousef Yousef 2013 年 11 月 25 日
I want it in general case,like any matrix in this form

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

a=[5 5 10 10 2 4 5],
clear d;
[u,~,iy] = unique(a,'stable');
for ii = numel(u):-1:1
lhs = [u(ii) find(iy==ii).'];
d(ii,1:numel(lhs)) = lhs;
end

6 件のコメント

Image Analyst
Image Analyst 2013 年 11 月 25 日
編集済み: Image Analyst 2013 年 11 月 25 日
I knew it . They are getting very close to finishing the Crystal Ball Toolbox. Sean, care to say when it will be released, or is it still a closely held secret?
Walter Roberson
Walter Roberson 2013 年 11 月 25 日
It can be done without using find() at all, by using bsxfun(@eq) and logical indexing that against 1:length(a)
yousef Yousef
yousef Yousef 2013 年 11 月 25 日
Thanks .Its working well ,just its too complicated.I will be so glad if you explain the main steps in 1 or 2 words
yousef Yousef
yousef Yousef 2013 年 11 月 25 日
編集済み: Walter Roberson 2013 年 11 月 26 日
It works well in small matrices but if the matrix is big such as randi(700,10,20),I found a repetitions in one column while in my case this repetition is not desired.
6 1 2 12 15
10 3 7 18 0
4 4 5 9 0
1 6 16 0 0
2 8 19 13 0
7 10 11 0 0
8 13 0 0 0
5 14 20 0 0
3 17 20 0 0
the repetitions are normal in rows but forbidden in columns,so how to solve this problem.
20 is repeated in the third column
yousef Yousef
yousef Yousef 2013 年 11 月 25 日
if you want to see the results and what I mean exactly ,just write your email to send you the results.
Sean de Wolski
Sean de Wolski 2013 年 11 月 26 日
編集済み: Sean de Wolski 2013 年 11 月 26 日
@Walter, I'd be curious to time bsxfun(...) against find. I agree that any opportunity for a bsxfun, it should be used just 'cuz it's awesome.
@IA, you know how silent we are on future features :)
@Yousef, I do see the duplicated 20 in column two above. But I don't know how you got it. Please give me full reproduction steps.
When I run something like the following, it passes as I expect:
a = randi(700,[1 10000]);
% a=[5 5 10 10 2 4 5],
clear d;
[u,~,iy] = unique(a,'stable');
for ii = numel(u):-1:1
lhs = [u(ii) find(iy==ii).'];
d(ii,1:numel(lhs)) = lhs;
end
T = matlab.unittest.TestCase;
import matlab.unittest.constraints.IsEqualTo
T.verifyThat(ismember(1:numel(a),d(:,2:end)),IsEqualTo(true(1,numel(a))));
T.verifyThat(nnz(d(:,2:end)),IsEqualTo(numel(a)));
Interactive verification passed.
Interactive verification passed.

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

Andrei Bobrov
Andrei Bobrov 2013 年 11 月 26 日
編集済み: Andrei Bobrov 2013 年 11 月 26 日
[a1,b,c] = unique(a(:),'first')
[~,ii] = sort(b);
a1 = a1(ii);
c = ii(c);
n = max(histc(c,(1:max(c))'));
f1 = @(x){[x(:)',zeros(1,n-numel(x))]};
p = accumarray(c,(1:numel(c))',[],f1)
out = [a1, cat(1,p{:})];
or with Nan
[a1,b,c] = unique(a(:),'first')
[~,ii] = sort(b);
a1 = a1(ii);
c = ii(c);
idx = bsxfun(@times,1:numel(a),bsxfun(@eq,a1,a));
idx(idx==0) = nan;
ii = sort(idx,2);
out = [a1,ii(:,~all(isnan(ii)))];
Roger Stafford
Roger Stafford 2013 年 11 月 27 日
編集済み: Roger Stafford 2013 年 11 月 27 日
Here's another version. Let a be your array and d be the desired result.
[u,t,q] = unique(a(:),'first','stable');
[q,p] = sort(q);
r = p;
n = size(p,1);
r(p) = (1:n)';
t = r(t);
r = cumsum(1-accumarray(t(2:end),diff(t),[n,1]));
d = [i.accumarray([q,r],p)];

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

質問済み:

2013 年 11 月 25 日

編集済み:

2013 年 11 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by