Filter löschen
Filter löschen

How to create an original matrix by using non zero value indices?

1 Ansicht (letzte 30 Tage)
A = [1 0 0 0 1;
1 0 0 0 1 ;
1 0 0 0 0;
1 0 0 0 0;
1 0 0 0 0];
[R1 C1] = size(A); %% Size of A is 5 x 5
[X, Y] = find(A);
P = [X, Y];
P = [ 1 1;
2 1;
3 1;
4 1;
5 1;
1 5;
2 5];
[R2 C2] = size(P); %% Size of P is 7 x 2
How to create an original matrix (A) of the size 5 x 5 by using non zero value indices of variable P of the size 7 x 2 and remaining indices values are zero.

Akzeptierte Antwort

yanqi liu
yanqi liu am 20 Feb. 2021
sir, may be use the follows code
clc; clear all; close all;
A = [1 0 0 0 1;
1 0 0 0 1 ;
1 0 0 0 0;
1 0 0 0 0;
1 0 0 0 0];
%% Size of A is 5 x 5
[R1, C1] = size(A);
[X, Y] = find(A);
P = [X, Y];
P = [ 1 1;
2 1;
3 1;
4 1;
5 1;
1 5;
2 5];
%% Size of P is 7 x 2
[R2 C2] = size(P);
%% gen matrix
B = zeros(max(P(:,1)), max(P(:,2)));
loc = sub2ind(size(B), P(:,1), P(:,2));
B(loc) = 1;
disp(B)
isequal(A, B)
1 0 0 0 1
1 0 0 0 1
1 0 0 0 0
1 0 0 0 0
1 0 0 0 0
ans =
logical
1
>>
  1 Kommentar
SUKUMAR NAGINENI
SUKUMAR NAGINENI am 20 Feb. 2021
Bearbeitet: SUKUMAR NAGINENI am 20 Feb. 2021
Dear yanqi liu
Thank you so much sir for your help,
you can read the book 《计算机视觉与深度学习实战 , This textbook link is not available on any website in India. If possible, kindly share the link to the soft copy of the book.
Thanks & regards
Sukumar.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by