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

3 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 23 Nov. 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))

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by