我正在学习如何使用numpy矩阵函数,我遇到了问题。
我有一个n个整数的列表,以及一个n行的矩阵。我需要将列表中的每个数字添加到矩阵中相应行的开头。
所以,如果我有以下矩阵和列表:
newMatrix = [[10, 0, 13],
[11, 13, 0]]
我想要的输出是:
for c in range(len(myList)):
newMatrix = np.insert(m[c],[0],myList[c])
这是我到目前为止的代码(尝试复制this page上的最后一个示例):
package com.apps.androidapps10;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.os.Environment;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.view.View;
import android.widget.Spinner;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
Button hantarBtn;
private static final String TAG = "MEDIA";
String namaJalan = "";
String namaLorong = "";
String noKenderaan;
private String filename = "SampleFile.txt";
private String filepath = "MyFileStroage";
File myExternalFile;
EditText inputText;
String separator = System.getProperty("line.separator");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_main2);
hantarBtn = (Button) findViewById(R.id.button);
inputText = (EditText) findViewById(R.id.editText);
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader
(getAssets().open("jalan.txt")));
ArrayList<String> jalan = new ArrayList<String>();
String line = bReader.readLine();
while (line != null){
jalan.add(line);
line = bReader.readLine();
}
bReader.close();
Spinner spinner1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,jalan);
spinner1.setAdapter(adapter1);
namaJalan = spinner1.getSelectedItem().toString();
}catch (IOException e){
e.printStackTrace();
}
try {
BufferedReader bReader1 = new BufferedReader(new InputStreamReader
(getAssets().open("lorong.txt")));
ArrayList<String> lorong = new ArrayList<String>();
String line = bReader1.readLine();
while (line != null){
lorong.add(line);
line = bReader1.readLine();
}
bReader1.close();
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,lorong);
spinner2.setAdapter(adapter2);
namaLorong = spinner2.getSelectedItem().toString();
}catch (IOException e) {
e.printStackTrace();
}
hantarBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
FileOutputStream fos = new FileOutputStream(myExternalFile,true);
fos.write(inputText.getText().toString().getBytes());
fos.write("\t".getBytes());
fos.write(namaJalan.toString().getBytes());
fos.write("\t".getBytes());
fos.write(namaLorong.toString().getBytes());
fos.write("\r\n".getBytes());
fos.flush();
fos.close();
Toast.makeText(getApplicationContext(), "Write Done...",
Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(Main2Activity.this, Main2Activity.class);
//startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Log.i(TAG, "Failed to write");
}
}
});
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
hantarBtn.setEnabled(false);
} else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
}
private static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
private static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}
但这当然只给出了for循环的最后一次迭代([11,13,0])。我想以某种方式将每一行附加到一个新的矩阵,但我似乎无法弄明白。
编辑:列表和矩阵的长度并不总是已知。
如果对numpy matrices更有经验的人知道更好的方法,我真的很感激!提前谢谢。
答案 0 :(得分:2)
我的解决方案是:
import numpy as np
m = np.matrix([[0, 13], [13, 0]])
myList = [10, 11]
newmatrix = np.insert(m, 0, myList, axis=1)
输出是:
[[10 0 13]
[11 13 0]]
答案 1 :(得分:1)
一种选择是重塑myList
,然后使用np.concatenate()
功能:
import numpy as np
np.concatenate((np.array(myList).reshape(len(myList),1), m), axis = 1)
# matrix([[10, 0, 13],
# [11, 13, 0]])
你也可以这样做:
np.concatenate((np.array(myList)[:, None], m), axis = 1)
# matrix([[10, 0, 13],
# [11, 13, 0]])