Selecting element at same position in a matrix

조회 수: 1 (최근 30일)
Joel Schelander
Joel Schelander 2021년 4월 13일
댓글: Joel Schelander 2021년 4월 13일
VID1 is a 3x3 cell. I choose a value randomly from the cell like thus.
ID1=VID1{ randi([1,size(VID1,1)],1), randi([1,size(VID1,2)],1 )};
If I have randomly chosen element VID{1,1}. I want to choose an element in the same position in another cell Vehicle1 (3x3 cell). How can I do this?

채택된 답변

Jan
Jan 2021년 4월 13일
편집: Jan 2021년 4월 13일
If you need the indices again, store them in variables:
i1 = randi([1, size(VID1, 1)]);
i2 = randi([1, size(VID1, 2)]);
ID1 = VID1{i1, i2};
V1 = Vehicle1{i1, i2};
It might be easier to use linear indices:
index = randi([1, numel(VID1)]);
ID1 = VID1{index};
V1 = Vehicle1{index};
  댓글 수: 1
Joel Schelander
Joel Schelander 2021년 4월 13일
Hi @Jan thank you, that helps! However now I get different values compared to my previous method. Was my previous method wrongly stated? I thought the result INCREASE2 (I later save INCREASE2 in a cell) should contain the same values as before.
Just asking if you can see something obvious
Before:
for jj=1:numel(VID1)
V11=Vehicle1{jj};
ID1=VID1{jj};
for jj2=1:numel(VID2)
V22=Vehicle2{jj2};
ID2=VID2{jj2};
%Removes all doubles
if numel(intersect(ID1,ID2))
continue
end
INCREASE2{jj,jj2}=max(HH2+V11+V22)./max(HH2);
IDcell2{jj,jj2}=[ID1 0 ID2];
end
end
After:
for o=1:numel(INCREASE2)
for oz=1:999
k1 = randi([1, size(VID1, 1)]);
kk1 = randi([1, size(VID1, 2)]);
ID1 = VID1{k1, kk1};
V1 = Vehicle1{k1, kk1};
k2 = randi([1, size(VID2, 1)]);
kk2 = randi([1, size(VID2, 2)]);
ID2 = VID2{k2, kk2};
V2 = Vehicle1{k2, kk2};
if numel(intersect(ID1,ID2))
continue
else
IDCELL{o}={ID1; ID2};
INCREASE2{o}=max(HH2+V1+V2)./max(HH2);
break
end
end
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by