recreate array original

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

recreate array original

turbofib
hi,
look this:

a=[2 6 1;2 5 8;3 1 6]

[b,c]=find(a>3);

b= 1
    2
    2
    3

c=  2
     2
     3
     3

d=zeros(size(a));

I want to write a new matrix of zeros but keeping the values ​​found with
find

0 6 0
0 5 8
0 0 6

any help me?^ thank  (i see help find but i not found answer)






--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: recreate array original

Nir Krakauer-3
d = a .* (a > 3)


Reply | Threaded
Open this post in threaded view
|

Re: recreate array original

nrjank

On Thu, Aug 1, 2019 at 2:07 PM Nir Krakauer <[hidden email]> wrote:
d = a .* (a > 3)


turbofib, 

recommend you look at what the individual parts of Nir's suggestion do.  it's a combination of logical indexing and elementwise operations.  Also recommend you look up some tips on array, linear, and logical indexing.  see:  https://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
 


Reply | Threaded
Open this post in threaded view
|

Re: recreate array original

nrjank
In reply to this post by Nir Krakauer-3

On Thu, Aug 1, 2019 at 2:22 PM gigiolone123 <[hidden email]> wrote:
thank you for answer but i need to use b and c to ricreate matrix..


is this a homework assignment? maybe you should provide the complete bounds of what you want to do and we can provide some guidance. otherwise we may just propose more ways that won't work for you.  in particular, if you read "help find", you'll learn that if you get three vectors from find you can recreate it as a sparse matrix:

>> help find

"...

    If three outputs are requested, 'find' also returns a vector
     containing the nonzero values.  For example:
          [i, j, v] = find (3 * eye (2))
                 => i = [ 1; 2 ]
                 => j = [ 1; 2 ]
                 => v = [ 3; 3 ]

     Note that this function is particularly useful for sparse matrices,
     as it extracts the nonzero elements as vectors, which can then be
     used to create the original matrix.  For example:
          sz = size (a);
          [i, j, v] = find (a);
          b = sparse (i, j, v, sz(1), sz(2));

"


Reply | Threaded
Open this post in threaded view
|

Re: recreate array original

gigiolone123
In reply to this post by nrjank
hi,
i look your site but i don't understand how can i do it..

i give you another example:

a =  1 3 4..
       4 2 1..
       2 4 5..
       .. . ..

I have several pairs of rows and columns

b :  1 3 ......(row)  
c:   2 3 ......(colum)

d=zeros(size(a));

for i=1:length(x)
   d(b(x),c(x))= a(b(x),c(x))
endfor

it's possible to do it avoid loop?

thank







--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: recreate array original

Dmitri A. Sergatskov
On Thu, Aug 1, 2019 at 2:18 PM gigiolone123 <[hidden email]> wrote:

>
> hi,
> i look your site but i don't understand how can i do it..
>
> i give you another example:
>
> a =  1 3 4..
>        4 2 1..
>        2 4 5..
>        .. . ..
>
> I have several pairs of rows and columns
>
> b :  1 3 ......(row)
> c:   2 3 ......(colum)
>
> d=zeros(size(a));
>
> for i=1:length(x)
>    d(b(x),c(x))= a(b(x),c(x))
> endfor
>
> it's possible to do it avoid loop?
>
> thank
>
>

You can convert your original matrix into vector aa= a(:); do find thing
and then recover the aa vector and then reshape() it back to original
matrix.

Dmitri.
--