返回满足条件的三维矩阵中的变量索引

时间:2017-10-13 18:10:47

标签: matlab

我试图找到满足条件的3个变量的值,即它们的和小于或等于1.我的方法是使用ndgrid来扫描变量的所有组合并定义矩阵I,如果条件满足,则包含1。我的代码在

之下
ss=0.25;
[pp1,pp2,pp3] = ndgrid(0:ss:1,0:ss:1,0:ss:1);
I = pp1+pp2+pp3<=1

我的问题是,如何生成所有有效变量组合的列表?我希望3 x n向量p包含npp1pp2的所有pp3有效值。

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我将其作为答案发布。它只是

public class WidgetDataRemoteimplements RemoteViewsService.RemoteViewsFactory {
Context context;
Intent intent;
ArrayList<String> date = new ArrayList<>();
ArrayList<String> subj = new ArrayList<>();
ArrayList<String> type = new ArrayList<>();
ArrayList<String> comment = new ArrayList<>();
JSONParser jsonParser = new JSONParser();
ArrayList<String> subjFromDb = new ArrayList<>();
int pippo = 0;

public WidgetDataRemote(Context context, Intent intent) {
    this.context = context;
    this.intent = intent;
}

@Override
public RemoteViews getViewAt(int position) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.item_widget);
    remoteViews.setTextViewText(R.id.task_data, date.get(position));
    remoteViews.setTextViewText(R.id.task_subject, subj.get(position));
    remoteViews.setTextViewText(R.id.task_type, type.get(position));
    remoteViews.setTextViewText(R.id.task_comment, comment.get(position));
    return remoteViews;
}

@Override
public void onCreate() {
    new LoadSubjects().execute();
}

@Override
public void onDataSetChanged() {
}

@Override
public void onDestroy() {
    date.clear();
    subj.clear();
    type.clear();
    comment.clear();
}

@Override
public int getCount() {
    return date.size();
}

@Override
public RemoteViews getLoadingView() {
    return null;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public long getItemId(int id) {
    return id;
}

@Override
public boolean hasStableIds() {
    return true;
}

class LoadSubjects extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        subjFromDb.clear();
        pippo = 0;
    }

    @Override
    protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("class", User.getClasse()));
        JSONArray json = jsonParser.makeHttpRequestArray(Constants.URL_LOAD_SUBJECTS, "POST", params);
        try {
            if (!json.toString().equals("[null]")) {
                for (int i = 0; i < json.length(); i++) {
                    JSONObject c = json.getJSONObject(i);
                    String subj = c.getString(Constants.TAG_NAME);
                    subjFromDb.add(subj);
                }
            } else {
                pippo = 1;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        if (pippo == 0) {
            new LoadTasks().execute();
        }
    }

    class LoadTasks extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pippo = 0;
        }

        @Override
        protected String doInBackground(String... args) {
            List<NameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("id_class", User.getClasse()));
            JSONArray json = jsonParser.makeHttpRequestArray(Constants.URL_LOAD_TASKS, "POST", params);
            try {
                if (!json.toString().equals("[null]")) {
                    for (int i = 0; i < json.length(); i++) {
                        JSONObject c = json.getJSONObject(i);
                        Date data = null;
                        try {
                            data = new SimpleDateFormat("yyyy-MM-dd").parse(c.getString(Constants.TAG_DATE));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        String newDate = new SimpleDateFormat("dd/MM/yyyy").format(data);
                        if (subjFromDb.contains(c.getString(Constants.TAG_SUBJECT))) {
                            date.add(newDate);
                            subj.add(c.getString(Constants.TAG_SUBJECT));
                            type.add(c.getString(Constants.TAG_TYPE));
                            comment.add(c.getString(Constants.TAG_COMMENT));
                        }
                    }
                } else {
                    pippo = 1;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void onPostExecute(String file_url) {
            if (pippo == 0){
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                ComponentName thisWidget = new ComponentName(context, WidgetDataProvider.class);
                int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
                appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_listView);
                // I use this to update the listview after fetch data. Is it correct?
            }
        }
    }
}}

如果有人有更好的解决方案,我将不胜感激。