Matrix Manipulation

1 次查看(过去 30 天)
Amandeep
Amandeep 2011-9-8
Hi,
From the following matrix:
A = [1; 1; 3; 1; 2; 1; 1; 1; 3; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 3; 1; 1; 1; 2; 1; 1; 1; 1; 1; 2; 2; 1; 1; 1; 2; 1; 1; 2; 1; 1; 1; 1; 1; 1; 3; 1; 1; 3; 2; 1; 1; 1; 1; 1; 1; 1; 2; 2; 4; 1; 1; 2; 1; 1; 1; 1; 2; 2; 1; 1; 1; 3]
I want a matrix of (row numbers minus one) where A is greater than 1. Also, if the element in A is 2 for example then B for that element in A will record the (row numbers minus one) once, if it was a 3 then just twice, and so on; therfore B will look like:-
B = [2, 2, 4, 8, 8, 13, 19, 19,....., 71, 71]
Thanks, any help will be great!

采纳的回答

Paulo Silva
Paulo Silva 2011-9-8
A = [1; 1; 3; 1; 2; 1; 1; 1; 3; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 3; 1; 1; 1; 2; 1; 1; 1; 1; 1; 2; 2; 1; 1; 1; 2; 1; 1; 2; 1; 1; 1; 1; 1; 1; 3; 1; 1; 3; 2; 1; 1; 1; 1; 1; 1; 1; 2; 2; 4; 1; 1; 2; 1; 1; 1; 1; 2; 2; 1; 1; 1; 3];
a1=find(A==2)-1; %find the index of the 2
a2=find(A==3)-1; %find the index of the 3
a2=repmat(a2,2,1) %duplicate the 3 indexes
B=sort([a1;a2])'; %join and sort the solution, also transpose
%I transposed B because of your B example
  3 个评论
Paulo Silva
Paulo Silva 2011-9-8
it was on purpose, that way you need to understand my code and add your code to it ;)
Amandeep
Amandeep 2011-9-8
lol cheers

请先登录,再进行评论。

更多回答(1 个)

Amandeep
Amandeep 2011-9-8
I think I figured it usin arrayfun:
A = [1; 1; 3; 1; 2; 1; 1; 1; 3; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 3; 1; 1; 1; 2; 1; 1; 1; 1; 1; 2; 2; 1; 1; 1; 2; 1; 1; 2; 1; 1; 1; 1; 1; 1; 3; 1; 1; 3; 2; 1; 1; 1; 1; 1; 1; 1; 2; 2; 4; 1; 1; 2; 1; 1; 1; 1; 2; 2; 1; 1; 1; 3];
missing = (A==1);
A(missing) = 0;
[row, col, vec] = find(A);
b = arrayfun(@(i) (row(i,1)-1)*(ones(1,vec(i,1)-1)),1:size(row,1),'uniform',0)
b = cat(2,b{:});;
  1 个评论
Andrei Bobrov
Andrei Bobrov 2011-9-8
A1=A-1;
[i1,~,v]=find(A1)
B = cell2mat(arrayfun(@(x,y)x*ones(1,y),i1'-1,v','un',0))

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by