matrix indexcing in compare with two matrices

Hello
I have 3 matrices:
m_z=[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0]
m_y=[19 6 21 16 5 17 3 25 9 20 8 24 15 11 12 4 23 7 14 13 18 2 1 10 22]
m_x=[9 13 14 17 1 2 3 8 21 6 11 12]
I want to run
A=m_z(m_y)
except about the numbers that are in m_x. about these numbers, elemnts of matrix A must be zero.
Thank you very much

 채택된 답변

Guillaume
Guillaume 2015년 1월 17일

0 개 추천

Wouldn't this do what you want:
m_z(m_x) = 0; %set elements m_x of m_z to 0 so when they're indexed by m_y they return 0
A = m_z(m_y);

댓글 수: 5

No, in fact matrix m_x is come from the index of cells that are located by 1 in matrices a, b, c (in my following code)
from my code you can find that number of elements 1 in matrix a , b and c are 4, 3 and 5 respectively.
I want to create matrix d randomly with 6 elements 1 in cells that have not been filled by one before with matrix a and b and c.
I think at the end of my code if I eliminate the number of cells that are located with 1 in matrix a and b and c from m_y I can create matrix d.
if true
function[d]=random(d_area)
a=[0 0 0 0 0;0 0 0 1 0;0 0 1 0 0;0 1 1 0 0;0 0 0 0 0]
b=[1 0 0 0 1;1 0 0 0 0;1 1 0 0 0;0 0 0 0 0;0 0 0 0 0]
c=[0 1 1 0 0;0 0 1 0 0;0 0 0 0 0;0 0 0 0 0;0 0 0 0 0]
[m n]=size(a)
aa1=numel(find (a>0))
bb1=numel(find (b>0))
cc1=numel(find (c>0))
d_area=6 %number of elements that must be 1 in matrix d
A1=aa1+bb1+cc1+d_area
aa2=numel(find(find((a>0))>A1))
bb2=numel(find(find((b>0))>A1))
cc2=numel(find(find((c>0))>A1))
B=aa2+bb2+cc2
A=A1-B
dimen=m*n;
m_y=randperm(dimen);
z1=[ones(1,A), zeros(1,dimen-A)];
z=reshape(z1,m,n)
d1=a+b+c+z
d end
Guillaume
Guillaume 2015년 1월 17일
편집: Guillaume 2015년 1월 17일
Ah, ok. You should given that explanation from the beginning. The simplest way to do this:
a = [0 0 0 0 0;0 0 0 1 0;0 0 1 0 0;0 1 1 0 0;0 0 0 0 0]
b = [1 0 0 0 1;1 0 0 0 0;1 1 0 0 0;0 0 0 0 0;0 0 0 0 0]
c = [0 1 1 0 0;0 0 1 0 0;0 0 0 0 0;0 0 0 0 0;0 0 0 0 0]
validindices = setdiff(1:numel(a), [find(a); find(b); find(c)]);
pick = randperm(numel(validindices), 6); %6 indices to pick
d = zeros(size(a));
d(validindices(pick)) = 1
Basically, use setdiff to get the indices you can pick from (set difference of all indices minus the ones already used in a, b and c) and pick 6 from there.
Thank you but when I use
pick=randperm(numel(validindices),6)
I got this error
??? Error using ==> randperm
Too many input arguments.
You must have a fairly old version of matlab, then. The two argument version of randperm has been available since R2011b. Just use randperm with 1 argument and use the first 6 elements.
pick = randperm(numel(validindices);
pick = pick(1:6);
Thank you for your kindness

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

질문:

2015년 1월 17일

댓글:

2015년 1월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by