A problem based on Matrix Division

3 ビュー (過去 30 日間)
Jalashwa
Jalashwa 2015 年 5 月 17 日
回答済み: charu sharma 2015 年 8 月 20 日
Write a function called divisible that takes a matrix A of positive integers and a single positive integer k as its two inputs and returns a matrix B that has the same size as A. The elements of B are all divisible by k. If an element of A is divisible by k, then the corresponding element in B must have the same value. If an element of A is not divisible by k, then the corresponding element of B must be the product of the given element of A and k. You are not allowed to use any for-­‐loops or while-­‐loops. For example, the call
X = divisible([1 2 ; 3 4], 2)
would make X equal to [2 2 ; 6 4].
  2 件のコメント
Geoff Hayes
Geoff Hayes 2015 年 5 月 17 日
Jalashwa - rather than posting your homework assignment/problem verbatim, why not describe what you have tried so far or what you don't understand that is preventing you from moving forward?

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2015 年 5 月 17 日
Logical indexing.

charu sharma
charu sharma 2015 年 8 月 20 日
There is no need of any loop, logical indexing can be used and it can be done in one line. Refer this with explanation: http://farzicoders.blogspot.in/2015/08/write-function-called-divvy-that-takes.html

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by