How to resolve issue while dealing with larger matrices?

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 Comments

No, its not same even for a smaller matrix. try to run several times for case 1. C and V are not same every time as you are working with random number.
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) ;
isequal(C,V)
ans = logical
1
%%
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) ;
isequal(C,V)
ans = logical
0
@Arif Hoq thank you, but is there any way to get back V from B?
I am not sure about your expectation. but as you are working with random number you need to run several times to get the V from B. still need to develop more.
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
Sorry, I'm not getting your point , while using random numbers the output varies with in each iteration.
But when I run only one time, and name them with a varable then it is fixed, until I run it next time. So in this case there should be a posibiliy to get V back with B.
A=randi(16,[4,4]);
B=A(:)';
P=randi(16,[1 4]);
V=randi(16,[1 4]);
B(P)=V;
But when I run only one time, and name them with a varable then it is fixed, until I run it next time
can you please show the result ?
A=randi(16,[4,4])
B=A(:)'
P=randi(16,[1 4])
V=randi(16,[1 4])
B(P)=V;
A =
1 3 11 13
4 7 12 7
7 3 13 6
4 13 2 8
B =
1 4 7 4 3 7 3 13 11 12 13 2 13 7 6 8
P =
16 16 13 2
V =
2 13 8 7
Now, I wanted to say that these are B,P,V, are fixed now,how to further operate these for required purpose
You want to get V from B, right ? only the the value of V(here 2 13 8 7) ? and how about other values in B( here 1 4 7 4 3 7 3 13 11 12 13 2 13 7 6 8 ) ? others would be 0 ?
@Arif Hoq Got the point why I'm facing the problem, thank you very much. How can I vote your answers?
i am posting my previous response as an answer

Sign in to comment.

Answers (2)

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(:)'
B = 1×16
8 9 11 8 9 4 5 11 1 14 13 5 1 15 13 5
P=randi(16,[1 4])
P = 1×4
7 1 8 8
V=randi(16,[1 4])
V = 1×4
2 11 13 4
B(P)=V % replace entries at position P with values in V
B = 1×16
11 9 11 8 9 4 2 4 1 14 13 5 1 15 13 5
C=B(1,P)
C = 1×4
2 11 4 4
C==V
ans = 1×4 logical array
1 1 0 1
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.
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

Asked:

on 25 Mar 2022

Commented:

on 25 Mar 2022

Community Treasure Hunt

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

Start Hunting!