Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:55:18+00:00 2026-06-11T10:55:18+00:00

Suppose now I have a matrix S = [1 1 1 2 2 2;

  • 0

Suppose now I have a matrix

S = [1 1 1 2 2 2;
     1 1 1 2 2 2;
     2 2 2 2 1 1;
     2 2 2 2 1 1;
     2 2 2 2 1 1]

And another matrix

A = [1 2;
     2 4]

The first row in A is the unique indices of S, and the second row contains the values that the values in the first row will be replaced. That is, all “1”s in S will be replaced by 2, and all “2”s will be replaced by 4. Finally I’ll get a matrix

SS = [2 2 2 4 4 4;
      2 2 2 4 4 4;
      4 4 4 4 2 2;
      4 4 4 4 2 2;
      4 4 4 4 2 2]

Right now what I’m doing is:

SS = zeros(size(S));
for i = 1:size(A,2)
    SS(S==index(A(1, i)) = A(2,i);
end

Now, I have a pretty big matrix, and using a for loop is a little bit slow. Is there a faster way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:55:20+00:00Added an answer on June 11, 2026 at 10:55 am

    Use the second output of ismember to give you indices of the values in row 1 of A. Use these indices to directly create matrix SS.

    Example (changed initial values for clarity):

    S = [5 5 5 3 3 3;
    5 5 5 3 3 3;
    3 3 3 3 5 5;
    3 3 3 3 5 5;
    3 3 3 3 5 5];
    A = [5 3;
    2 4];

    >> [~, Locb] = ismember(S,A(1,:))
    Locb =
    
         1     1     1     2     2     2
         1     1     1     2     2     2
         2     2     2     2     1     1
         2     2     2     2     1     1
         2     2     2     2     1     1
    
    >> SS = reshape(A(2,Locb),size(S))
    SS =
    
         2     2     2     4     4     4
         2     2     2     4     4     4
         4     4     4     4     2     2
         4     4     4     4     2     2
         4     4     4     4     2     2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
this is a follow-up question of mine. Suppose now I have a URL :
Suppose I have a sting as PHP Paddy,PHP Pranav,PHP Parth, now i have a
Suppose I have this feature branch foo. Now I want to merge it back
Suppose i have 1kk records in my database. Now i need to select some
Suppose I have a web application, which uses jQuery for Ajax and UI. Now
I know that .NET assembly is self-descriptive because of the metadata. Now suppose I
Suppose I have a rather large class Matrix , and I've overloaded operator== to
Suppose I have a function that does something on an arbitrary container type (C++11):
http://en.wikipedia.org/wiki/Rotation_matrix Suppose i have (x,y) 2D pixels for image, now i want to rotate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.