How to resolve issue while dealing with larger matrices?
1 次查看(过去 30 天)
显示 更早的评论
A=randi(16,[4,4]);
B=A(:)';
P=randi(16,[1 4]);
V=randi(16,[1 4]);
B(P)=V; % replace entries at position P with values in V
C=B(1,P)
C=V satisfied in this case
But I'm not getting the right answer in case of larger size matrices.
A=randi(255,[512,512]);
>> B=A(:)';
>> P=randi(255,[1 256]);
>> V=randi(256,[1 256]);
>> B(P)=V; %
>> C=B(1,P);
C should be equal to B , but I'm not getting it right,
Request for help regarding this.
9 个评论
回答(2 个)
Voss
2022-3-25
As @Arif Hoq points out, the problem is not with the size of the matrices. The problem is that the elements of P are not guaranteed to be unique. Look at what happens when a number occurs more than once in P, in this case 8 appears twice in P:
A=randi(16,[4,4]);
B=A(:)'
P=randi(16,[1 4])
V=randi(16,[1 4])
B(P)=V % replace entries at position P with values in V
C=B(1,P)
C==V
In this case, B(P)=V is B([7 1 8 8]) = [2 11 13 4]; which means that B(8) is assigned the value 13 and then immediately assigned the value 4, so that B(8) is 4 after that step.
Then C = B(1,P) is C = B(1,[7 1 8 8]) = [2 11 4 4] not [2 11 13 4] = V
Any time P has repeated values, then V will not be recoverable from B and P.
Arif Hoq
2022-3-25
A=randi(16,[4,4]);
B=A(:)';
P=randi(16,[1 4]);
V=randi(16,[1 4]);
[C out]=ismember(V,B);
if numel(out(out~=0)) ==4
disp(out)
else
disp('V is not the member of B')
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!