How to output zeros from a matrix with corresponding matrix of indices for those indeces which are 0 or with negative sign ?

4 visualizaciones (últimos 30 días)
Hello
Suppose we have a nonsymetric Toeplitz matrix with zeros or negative numbers. In my case this Toeplitz matrix (might be any size) is a matrix of indicies.
For example:
A = [
0 -1
1 0
2 1
3 2]
Then I have a vector B, which is a signal
If I do
C=B(A)
I get, the following, which is expected:
Array indices must be positive integers or logical values.
But how to get a Matrix C with the same size as A and set to 0 those elements of C corresponding to zero or negative indeces of A with rest elements corresponding to "normal" indeces?
I tried with logic arrays, set to zero negative elements and find nonzero elements, but since one row can have "normal" indices and zero or negative ones, it did not work for me.
I guess/believe there is short and beautiful solution in Matlab.
Thank you!

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 23 de Nov. de 2020
Something like this will work
A = [
0 -1
1 0
2 1
3 2];
B = [1 5 10];
C = zeros(size(A));
C(A>0) = B(A(A>0))

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by