How to create an original matrix by using non zero value indices?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
SUKUMAR NAGINENI
el 19 de Feb. de 2021
Editada: SUKUMAR NAGINENI
el 20 de Feb. de 2021
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.
0 comentarios
Respuesta aceptada
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
Más respuestas (0)
Ver también
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!