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

1 visualización (últimos 30 días)
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.

Respuesta aceptada

yanqi liu
yanqi liu el 20 de Feb. de 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 comentario
SUKUMAR NAGINENI
SUKUMAR NAGINENI el 20 de Feb. de 2021
Editada: SUKUMAR NAGINENI el 20 de Feb. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by