Replace efficiently values in matrix without for-loops

조회 수: 1 (최근 30일)
Matteo
Matteo 2012년 3월 2일
Hi, This is my problem. Suppose you have a vector b:
b=[234 786 674 23];% the values are chosen at random for this example
and a matrix A:
A=[2 1;
4 4]; %the value of the elements of A should be <=length(b)
it exists a method or a function to obtain a second matrix B of the same dimensions of A but with the values replaced by the corresponding value in b?? For the previous example:
B=[786 234;
23 23]; % i.e., B(i,j)=b(A(i,j))
this is easily to do with at least one for-loop. but I am looking for a function or method that does this without for loops, maybe in a vectorized way (thus in an efficient way)!
Can someone help me??
Thank you,
Matteo

채택된 답변

Matteo
Matteo 2012년 3월 2일
ok sorry, it was a stupid question.
I hadn't noticed that b(A) does exactly what I wanted :)
Hope anyway this could be useful for other people.
Bye, Matteo

추가 답변 (2개)

the cyclist
the cyclist 2012년 3월 2일
Simple and elegant:
b(A)

Jonathan Sullivan
Jonathan Sullivan 2012년 3월 2일

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by