필터 지우기
필터 지우기

Compare elements in a matrix for a variable

조회 수: 5 (최근 30일)
R2
R2 2020년 6월 25일
댓글: R2 2020년 6월 26일
Hi
I want to compare the elements in a 3D matrix (10x6x31) to a varaible I have. If they match I would like to update the matrix and output a different value in that element corresponding to a different variable.
The two varaibles are related, where the first is the id (75x1) and the r value (75x1) corresponds to this.
This is what I have attempted but it doesnt seem to wrok.
id= material_data{1,1};
r= material_data{1,2};
j = numel(M_3D);
for i = 1:j
if M_3D(i)==id(:)
M_3D(i)=r;
end
end

답변 (1개)

Rik
Rik 2020년 6월 25일
편집: Rik 2020년 6월 25일
So essentially a lookup table?
%generate some random data
id= rand(75,1);
r= rand(75,1);
id_list=unique(id);
M_3D=id_list(randi([1 numel(id_list)],10,6,31));
%using the a variable is optional
[a,index_in_id]=ismember(M_3D,id);
if ~all(a(:))
error('missing indices')
end
replaced=r(index_in_id);
%confirm with loop
replaced2=M_3D;
for n=1:numel(replaced2)
ind= id==replaced2(n);
replaced2(n)=r(ind);
end
clc,assert(isequal(replaced,replaced2),'lookup failed')
  댓글 수: 1
R2
R2 2020년 6월 26일
It seems this requires all of the ids to match, what if it were to look for any that do but if some dont thats ok.

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

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by